diff --git a/intermediate/indexing/advanced-indexing.ipynb b/intermediate/indexing/advanced-indexing.ipynb index a5a8095e..9950e621 100644 --- a/intermediate/indexing/advanced-indexing.ipynb +++ b/intermediate/indexing/advanced-indexing.ipynb @@ -33,24 +33,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 1, 2, 3, 4, 5],\n", - " [ 6, 7, 8, 9, 10],\n", - " [11, 12, 13, 14, 15],\n", - " [16, 17, 18, 19, 20],\n", - " [21, 22, 23, 24, 25]])" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import numpy as np\n", "\n", @@ -68,822 +53,32 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray (x: 5, y: 5)>\n",
-       "array([[ 1,  2, ...,  4,  5],\n",
-       "       [ 6,  7, ...,  9, 10],\n",
-       "       ...,\n",
-       "       [16, 17, ..., 19, 20],\n",
-       "       [21, 22, ..., 24, 25]])\n",
-       "Dimensions without coordinates: x, y
" - ], - "text/plain": [ - "\n", - "array([[ 1, 2, ..., 4, 5],\n", - " [ 6, 7, ..., 9, 10],\n", - " ...,\n", - " [16, 17, ..., 19, 20],\n", - " [21, 22, ..., 24, 25]])\n", - "Dimensions without coordinates: x, y" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import xarray as xr\n", + "\n", "da = xr.DataArray(np_array, dims=[\"x\", \"y\"])\n", "da" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 1, 13, 25])" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "np_array [[0,2,4],[0,2,4]]" + "np_array[[0, 2, 4], [0, 2, 4]]" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray (dim_0: 3, dim_1: 3)>\n",
-       "array([[ 1,  3,  5],\n",
-       "       [11, 13, 15],\n",
-       "       [21, 23, 25]])\n",
-       "Dimensions without coordinates: dim_0, dim_1
" - ], - "text/plain": [ - "\n", - "array([[ 1, 3, 5],\n", - " [11, 13, 15],\n", - " [21, 23, 25]])\n", - "Dimensions without coordinates: dim_0, dim_1" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "da [[0,2,4],[0,2,4]]" + "da[[0, 2, 4], [0, 2, 4]]" ] }, { @@ -918,459 +113,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.Dataset>\n",
-       "Dimensions:  (lat: 25, time: 2920, lon: 53)\n",
-       "Coordinates:\n",
-       "  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0\n",
-       "  * lon      (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0\n",
-       "  * time     (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00\n",
-       "Data variables:\n",
-       "    air      (time, lat, lon) float32 241.2 242.5 243.5 ... 296.5 296.2 295.7\n",
-       "Attributes: (5)
" - ], - "text/plain": [ - "\n", - "Dimensions: (lat: 25, time: 2920, lon: 53)\n", - "Coordinates:\n", - " * lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0\n", - " * lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0\n", - " * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00\n", - "Data variables:\n", - " air (time, lat, lon) float32 241.2 242.5 243.5 ... 296.5 296.2 295.7\n", - "Attributes: (5)" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", @@ -1382,412 +129,14 @@ "\n", "ds = xr.tutorial.load_dataset(\"air_temperature\")\n", "da_air = ds.air\n", - "ds\n" + "ds" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray 'air' (lat: 4, lon: 3)>\n",
-       "array([[249.79999, 243.09999, 242.39   ],\n",
-       "       [274.29   , 271.79   , 270.6    ],\n",
-       "       [277.4    , 280.6    , 280.9    ],\n",
-       "       [283.19998, 285.5    , 285.9    ]], dtype=float32)\n",
-       "Coordinates:\n",
-       "  * lat      (lat) float32 70.0 65.0 50.0 42.5\n",
-       "  * lon      (lon) float32 202.5 215.0 217.5\n",
-       "    time     datetime64[ns] 2013-01-01\n",
-       "Attributes: (11)
" - ], - "text/plain": [ - "\n", - "array([[249.79999, 243.09999, 242.39 ],\n", - " [274.29 , 271.79 , 270.6 ],\n", - " [277.4 , 280.6 , 280.9 ],\n", - " [283.19998, 285.5 , 285.9 ]], dtype=float32)\n", - "Coordinates:\n", - " * lat (lat) float32 70.0 65.0 50.0 42.5\n", - " * lon (lon) float32 202.5 215.0 217.5\n", - " time datetime64[ns] 2013-01-01\n", - "Attributes: (11)" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "selected_da = da_air.isel(time=0, lat=[2, 4, 10, 13], lon=[1, 6, 7]) # -- orthogonal indexing\n", "selected_da" @@ -1811,476 +160,11 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray 'air' (time: 2920, degrees_north: 4, degrees_east: 4)>\n",
-       "array([[[293.1    , 293.1    , 293.29   , 293.29   ],\n",
-       "        [284.6    , 284.6    , 284.9    , 284.19998],\n",
-       "        [282.79   , 282.79   , 283.19998, 282.6    ],\n",
-       "        [282.79   , 282.79   , 283.19998, 282.6    ]],\n",
-       "\n",
-       "       [[293.19998, 293.19998, 293.9    , 294.19998],\n",
-       "        [283.29   , 283.29   , 285.19998, 285.19998],\n",
-       "        [281.4    , 281.4    , 282.79   , 283.5    ],\n",
-       "        [281.4    , 281.4    , 282.79   , 283.5    ]],\n",
-       "\n",
-       "       ...,\n",
-       "\n",
-       "       [[288.29   , 288.29   , 289.19   , 290.79   ],\n",
-       "        [282.09   , 282.09   , 281.59   , 282.38998],\n",
-       "        [280.99   , 280.99   , 280.38998, 280.59   ],\n",
-       "        [280.99   , 280.99   , 280.38998, 280.59   ]],\n",
-       "\n",
-       "       [[289.49   , 289.49   , 290.38998, 291.59   ],\n",
-       "        [282.09   , 282.09   , 281.99   , 283.09   ],\n",
-       "        [281.38998, 281.38998, 280.59   , 280.99   ],\n",
-       "        [281.38998, 281.38998, 280.59   , 280.99   ]]], dtype=float32)\n",
-       "Coordinates:\n",
-       "    lat      (degrees_north) float32 30.0 40.0 42.5 42.5\n",
-       "    lon      (degrees_east) float32 200.0 200.0 202.5 205.0\n",
-       "  * time     (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00\n",
-       "Dimensions without coordinates: degrees_north, degrees_east\n",
-       "Attributes: (11)
" - ], - "text/plain": [ - "\n", - "array([[[293.1 , 293.1 , 293.29 , 293.29 ],\n", - " [284.6 , 284.6 , 284.9 , 284.19998],\n", - " [282.79 , 282.79 , 283.19998, 282.6 ],\n", - " [282.79 , 282.79 , 283.19998, 282.6 ]],\n", - "\n", - " [[293.19998, 293.19998, 293.9 , 294.19998],\n", - " [283.29 , 283.29 , 285.19998, 285.19998],\n", - " [281.4 , 281.4 , 282.79 , 283.5 ],\n", - " [281.4 , 281.4 , 282.79 , 283.5 ]],\n", - "\n", - " ...,\n", - "\n", - " [[288.29 , 288.29 , 289.19 , 290.79 ],\n", - " [282.09 , 282.09 , 281.59 , 282.38998],\n", - " [280.99 , 280.99 , 280.38998, 280.59 ],\n", - " [280.99 , 280.99 , 280.38998, 280.59 ]],\n", - "\n", - " [[289.49 , 289.49 , 290.38998, 291.59 ],\n", - " [282.09 , 282.09 , 281.99 , 283.09 ],\n", - " [281.38998, 281.38998, 280.59 , 280.99 ],\n", - " [281.38998, 281.38998, 280.59 , 280.99 ]]], dtype=float32)\n", - "Coordinates:\n", - " lat (degrees_north) float32 30.0 40.0 42.5 42.5\n", - " lon (degrees_east) float32 200.0 200.0 202.5 205.0\n", - " * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00\n", - "Dimensions without coordinates: degrees_north, degrees_east\n", - "Attributes: (11)" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "target_lat = xr.DataArray([31, 41, 42, 42], dims=\"degrees_north\")\n", "target_lon = xr.DataArray([200, 201, 202, 205], dims=\"degrees_east\")\n", @@ -2319,392 +203,11 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray (points: 4)>\n",
-       "array([31, 41, 42, 42])\n",
-       "Dimensions without coordinates: points
" - ], - "text/plain": [ - "\n", - "array([31, 41, 42, 42])\n", - "Dimensions without coordinates: points" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Define target latitude and longitude (where weather stations might be)\n", "lat_points = xr.DataArray([31, 41, 42, 42], dims=\"points\")\n", @@ -2713,392 +216,11 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray (points: 4)>\n",
-       "array([200, 201, 202, 205])\n",
-       "Dimensions without coordinates: points
" - ], - "text/plain": [ - "\n", - "array([200, 201, 202, 205])\n", - "Dimensions without coordinates: points" - ] - }, - "execution_count": 45, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "lon_points = xr.DataArray([200, 201, 202, 205], dims=\"points\")\n", "lon_points" @@ -3113,428 +235,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray 'air' (time: 2920, points: 4)>\n",
-       "array([[293.1    , 284.6    , 283.19998, 282.6    ],\n",
-       "       [293.19998, 283.29   , 282.79   , 283.5    ],\n",
-       "       ...,\n",
-       "       [288.29   , 282.09   , 280.38998, 280.59   ],\n",
-       "       [289.49   , 282.09   , 280.59   , 280.99   ]], dtype=float32)\n",
-       "Coordinates:\n",
-       "    lat      (points) float32 30.0 40.0 42.5 42.5\n",
-       "    lon      (points) float32 200.0 200.0 202.5 205.0\n",
-       "  * time     (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00\n",
-       "Dimensions without coordinates: points\n",
-       "Attributes: (11)
" - ], - "text/plain": [ - "\n", - "array([[293.1 , 284.6 , 283.19998, 282.6 ],\n", - " [293.19998, 283.29 , 282.79 , 283.5 ],\n", - " ...,\n", - " [288.29 , 282.09 , 280.38998, 280.59 ],\n", - " [289.49 , 282.09 , 280.59 , 280.99 ]], dtype=float32)\n", - "Coordinates:\n", - " lat (points) float32 30.0 40.0 42.5 42.5\n", - " lon (points) float32 200.0 200.0 202.5 205.0\n", - " * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00\n", - "Dimensions without coordinates: points\n", - "Attributes: (11)" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "da_air.sel(lat=lat_points, lon=lon_points, method=\"nearest\")" ] @@ -3548,22 +253,11 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "('time', 'points')" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "da_air.sel(lat=lat_points, lon=lon_points, method=\"nearest\").dims" ] @@ -3581,464 +275,11 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray 'air' (time: 2920, lat: 3, points: 4)>\n",
-       "array([[[296.6    , 296.6    , 296.19998, 296.4    ],\n",
-       "        [293.1    , 293.1    , 293.29   , 293.29   ],\n",
-       "        [284.6    , 284.6    , 284.9    , 284.19998]],\n",
-       "\n",
-       "       [[296.4    , 296.4    , 295.9    , 296.19998],\n",
-       "        [293.19998, 293.19998, 293.9    , 294.19998],\n",
-       "        [283.29   , 283.29   , 285.19998, 285.19998]],\n",
-       "\n",
-       "       ...,\n",
-       "\n",
-       "       [[293.69   , 293.69   , 293.88998, 295.38998],\n",
-       "        [288.29   , 288.29   , 289.19   , 290.79   ],\n",
-       "        [282.09   , 282.09   , 281.59   , 282.38998]],\n",
-       "\n",
-       "       [[293.79   , 293.79   , 293.69   , 295.09   ],\n",
-       "        [289.49   , 289.49   , 290.38998, 291.59   ],\n",
-       "        [282.09   , 282.09   , 281.99   , 283.09   ]]], dtype=float32)\n",
-       "Coordinates:\n",
-       "  * lat      (lat) float32 20.0 30.0 40.0\n",
-       "    lon      (points) float32 200.0 200.0 202.5 205.0\n",
-       "  * time     (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00\n",
-       "Dimensions without coordinates: points\n",
-       "Attributes: (11)
" - ], - "text/plain": [ - "\n", - "array([[[296.6 , 296.6 , 296.19998, 296.4 ],\n", - " [293.1 , 293.1 , 293.29 , 293.29 ],\n", - " [284.6 , 284.6 , 284.9 , 284.19998]],\n", - "\n", - " [[296.4 , 296.4 , 295.9 , 296.19998],\n", - " [293.19998, 293.19998, 293.9 , 294.19998],\n", - " [283.29 , 283.29 , 285.19998, 285.19998]],\n", - "\n", - " ...,\n", - "\n", - " [[293.69 , 293.69 , 293.88998, 295.38998],\n", - " [288.29 , 288.29 , 289.19 , 290.79 ],\n", - " [282.09 , 282.09 , 281.59 , 282.38998]],\n", - "\n", - " [[293.79 , 293.79 , 293.69 , 295.09 ],\n", - " [289.49 , 289.49 , 290.38998, 291.59 ],\n", - " [282.09 , 282.09 , 281.99 , 283.09 ]]], dtype=float32)\n", - "Coordinates:\n", - " * lat (lat) float32 20.0 30.0 40.0\n", - " lon (points) float32 200.0 200.0 202.5 205.0\n", - " * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00\n", - "Dimensions without coordinates: points\n", - "Attributes: (11)" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "da_air.sel(lat=[20, 30, 40], lon=lon_points, method=\"nearest\")" ] @@ -4083,11 +324,6 @@ } ], "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, "language_info": { "codemirror_mode": { "name": "ipython", @@ -4097,8 +333,7 @@ "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.4" + "pygments_lexer": "ipython3" }, "toc": { "base_numbering": 1,