Skip to content

Commit

Permalink
update indexing redundancies
Browse files Browse the repository at this point in the history
  • Loading branch information
negin513 committed Jun 6, 2024
1 parent f25af4c commit 9ff571a
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions intermediate/indexing/advanced-indexing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -907,13 +907,6 @@
" The output of orthogonal indexing is a 3x3 array, while the output of vectorized indexing is a 1D array."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The principle of outer or orthogonal indexing is that the result mirrors the effect of independently indexing along each dimension with integer or boolean arrays, treating both the indexed and indexing arrays as one-dimensional."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1818,7 +1811,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 51,
"metadata": {
"tags": []
},
Expand Down Expand Up @@ -2216,7 +2209,7 @@
" 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)</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'>'air'</div><ul class='xr-dim-list'><li><span class='xr-has-index'>time</span>: 2920</li><li><span>degrees_north</span>: 4</li><li><span>degrees_east</span>: 4</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-ecf6ab3e-53c0-4fbc-99f7-8139574bbca8' class='xr-array-in' type='checkbox' checked><label for='section-ecf6ab3e-53c0-4fbc-99f7-8139574bbca8' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>293.1 293.1 293.3 293.3 284.6 284.6 ... 281.0 281.4 281.4 280.6 281.0</span></div><div class='xr-array-data'><pre>array([[[293.1 , 293.1 , 293.29 , 293.29 ],\n",
"Attributes: (11)</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'>'air'</div><ul class='xr-dim-list'><li><span class='xr-has-index'>time</span>: 2920</li><li><span>degrees_north</span>: 4</li><li><span>degrees_east</span>: 4</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-aad89412-a657-4270-9d88-0668b0a96145' class='xr-array-in' type='checkbox' checked><label for='section-aad89412-a657-4270-9d88-0668b0a96145' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>293.1 293.1 293.3 293.3 284.6 284.6 ... 281.0 281.4 281.4 280.6 281.0</span></div><div class='xr-array-data'><pre>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",
Expand All @@ -2236,10 +2229,10 @@
" [[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)</pre></div></div></li><li class='xr-section-item'><input id='section-2fe6f8f3-366b-4520-971a-5e2e664022d4' class='xr-section-summary-in' type='checkbox' checked><label for='section-2fe6f8f3-366b-4520-971a-5e2e664022d4' class='xr-section-summary' >Coordinates: <span>(3)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span>lat</span></div><div class='xr-var-dims'>(degrees_north)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>30.0 40.0 42.5 42.5</div><input id='attrs-44000c25-ea78-4372-bbe1-2042c8e86d5c' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-44000c25-ea78-4372-bbe1-2042c8e86d5c' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-fcf16f2c-cd50-4c7e-84b8-49524cb3782f' class='xr-var-data-in' type='checkbox'><label for='data-fcf16f2c-cd50-4c7e-84b8-49524cb3782f' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>long_name :</span></dt><dd>Latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd></dl></div><div class='xr-var-data'><pre>array([30. , 40. , 42.5, 42.5], dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>lon</span></div><div class='xr-var-dims'>(degrees_east)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>200.0 200.0 202.5 205.0</div><input id='attrs-286221c9-2c4a-4643-9d07-a819fd21f4b7' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-286221c9-2c4a-4643-9d07-a819fd21f4b7' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-8f2f655d-1fe0-489f-a1c5-cb38adee996e' class='xr-var-data-in' type='checkbox'><label for='data-8f2f655d-1fe0-489f-a1c5-cb38adee996e' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>standard_name :</span></dt><dd>longitude</dd><dt><span>long_name :</span></dt><dd>Longitude</dd><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd></dl></div><div class='xr-var-data'><pre>array([200. , 200. , 202.5, 205. ], dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>time</span></div><div class='xr-var-dims'>(time)</div><div class='xr-var-dtype'>datetime64[ns]</div><div class='xr-var-preview xr-preview'>2013-01-01 ... 2014-12-31T18:00:00</div><input id='attrs-382845eb-d7df-4604-aa4c-f31d9f7e7601' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-382845eb-d7df-4604-aa4c-f31d9f7e7601' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-b199475d-5109-4aba-936d-b7c35800b9c7' class='xr-var-data-in' type='checkbox'><label for='data-b199475d-5109-4aba-936d-b7c35800b9c7' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>standard_name :</span></dt><dd>time</dd><dt><span>long_name :</span></dt><dd>Time</dd></dl></div><div class='xr-var-data'><pre>array([&#x27;2013-01-01T00:00:00.000000000&#x27;, &#x27;2013-01-01T06:00:00.000000000&#x27;,\n",
" [281.38998, 281.38998, 280.59 , 280.99 ]]], dtype=float32)</pre></div></div></li><li class='xr-section-item'><input id='section-1e717edc-5cb5-4131-8386-d5a3a5bdccdf' class='xr-section-summary-in' type='checkbox' checked><label for='section-1e717edc-5cb5-4131-8386-d5a3a5bdccdf' class='xr-section-summary' >Coordinates: <span>(3)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span>lat</span></div><div class='xr-var-dims'>(degrees_north)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>30.0 40.0 42.5 42.5</div><input id='attrs-67efa0ef-9fda-4502-9099-dee465be013d' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-67efa0ef-9fda-4502-9099-dee465be013d' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-dba6cfa6-7beb-4a60-a0d1-05615577f823' class='xr-var-data-in' type='checkbox'><label for='data-dba6cfa6-7beb-4a60-a0d1-05615577f823' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>long_name :</span></dt><dd>Latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>axis :</span></dt><dd>Y</dd></dl></div><div class='xr-var-data'><pre>array([30. , 40. , 42.5, 42.5], dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>lon</span></div><div class='xr-var-dims'>(degrees_east)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>200.0 200.0 202.5 205.0</div><input id='attrs-a72b0083-6bb9-4991-b547-7d83dae6ecfc' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-a72b0083-6bb9-4991-b547-7d83dae6ecfc' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-63c2bd41-dff5-44b9-b715-263af2dfa6b3' class='xr-var-data-in' type='checkbox'><label for='data-63c2bd41-dff5-44b9-b715-263af2dfa6b3' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>standard_name :</span></dt><dd>longitude</dd><dt><span>long_name :</span></dt><dd>Longitude</dd><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>axis :</span></dt><dd>X</dd></dl></div><div class='xr-var-data'><pre>array([200. , 200. , 202.5, 205. ], dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>time</span></div><div class='xr-var-dims'>(time)</div><div class='xr-var-dtype'>datetime64[ns]</div><div class='xr-var-preview xr-preview'>2013-01-01 ... 2014-12-31T18:00:00</div><input id='attrs-2db855aa-b3e2-49e4-b674-1c3c6e3f6c34' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-2db855aa-b3e2-49e4-b674-1c3c6e3f6c34' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-38d9bba7-d1de-44ea-96c7-f2b01c8226ce' class='xr-var-data-in' type='checkbox'><label for='data-38d9bba7-d1de-44ea-96c7-f2b01c8226ce' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>standard_name :</span></dt><dd>time</dd><dt><span>long_name :</span></dt><dd>Time</dd></dl></div><div class='xr-var-data'><pre>array([&#x27;2013-01-01T00:00:00.000000000&#x27;, &#x27;2013-01-01T06:00:00.000000000&#x27;,\n",
" &#x27;2013-01-01T12:00:00.000000000&#x27;, ..., &#x27;2014-12-31T06:00:00.000000000&#x27;,\n",
" &#x27;2014-12-31T12:00:00.000000000&#x27;, &#x27;2014-12-31T18:00:00.000000000&#x27;],\n",
" dtype=&#x27;datetime64[ns]&#x27;)</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-69ddca08-0b63-4aa7-94c1-737005933536' class='xr-section-summary-in' type='checkbox' ><label for='section-69ddca08-0b63-4aa7-94c1-737005933536' class='xr-section-summary' >Indexes: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-index-name'><div>time</div></div><div class='xr-index-preview'>PandasIndex</div><div></div><input id='index-f58b2595-e9bf-4b09-8321-6791d3466895' class='xr-index-data-in' type='checkbox'/><label for='index-f58b2595-e9bf-4b09-8321-6791d3466895' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(DatetimeIndex([&#x27;2013-01-01 00:00:00&#x27;, &#x27;2013-01-01 06:00:00&#x27;,\n",
" dtype=&#x27;datetime64[ns]&#x27;)</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-4fe9e3cd-7875-4b60-bbcd-d9f38486cc5c' class='xr-section-summary-in' type='checkbox' ><label for='section-4fe9e3cd-7875-4b60-bbcd-d9f38486cc5c' class='xr-section-summary' >Indexes: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-index-name'><div>time</div></div><div class='xr-index-preview'>PandasIndex</div><div></div><input id='index-79b37a5d-e9ed-4552-8a7a-dbc201a3d7e9' class='xr-index-data-in' type='checkbox'/><label for='index-79b37a5d-e9ed-4552-8a7a-dbc201a3d7e9' title='Show/Hide index repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-index-data'><pre>PandasIndex(DatetimeIndex([&#x27;2013-01-01 00:00:00&#x27;, &#x27;2013-01-01 06:00:00&#x27;,\n",
" &#x27;2013-01-01 12:00:00&#x27;, &#x27;2013-01-01 18:00:00&#x27;,\n",
" &#x27;2013-01-02 00:00:00&#x27;, &#x27;2013-01-02 06:00:00&#x27;,\n",
" &#x27;2013-01-02 12:00:00&#x27;, &#x27;2013-01-02 18:00:00&#x27;,\n",
Expand All @@ -2250,7 +2243,7 @@
" &#x27;2014-12-30 12:00:00&#x27;, &#x27;2014-12-30 18:00:00&#x27;,\n",
" &#x27;2014-12-31 00:00:00&#x27;, &#x27;2014-12-31 06:00:00&#x27;,\n",
" &#x27;2014-12-31 12:00:00&#x27;, &#x27;2014-12-31 18:00:00&#x27;],\n",
" dtype=&#x27;datetime64[ns]&#x27;, name=&#x27;time&#x27;, length=2920, freq=None))</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-447e7d8f-fde3-437b-aa2e-125aa69eea88' class='xr-section-summary-in' type='checkbox' ><label for='section-447e7d8f-fde3-437b-aa2e-125aa69eea88' class='xr-section-summary' >Attributes: <span>(11)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'><dt><span>long_name :</span></dt><dd>4xDaily Air temperature at sigma level 995</dd><dt><span>units :</span></dt><dd>degK</dd><dt><span>precision :</span></dt><dd>2</dd><dt><span>GRIB_id :</span></dt><dd>11</dd><dt><span>GRIB_name :</span></dt><dd>TMP</dd><dt><span>var_desc :</span></dt><dd>Air temperature</dd><dt><span>dataset :</span></dt><dd>NMC Reanalysis</dd><dt><span>level_desc :</span></dt><dd>Surface</dd><dt><span>statistic :</span></dt><dd>Individual Obs</dd><dt><span>parent_stat :</span></dt><dd>Other</dd><dt><span>actual_range :</span></dt><dd>[185.16 322.1 ]</dd></dl></div></li></ul></div></div>"
" dtype=&#x27;datetime64[ns]&#x27;, name=&#x27;time&#x27;, length=2920, freq=None))</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-1d174dac-23ab-47db-8503-27c8366abb13' class='xr-section-summary-in' type='checkbox' ><label for='section-1d174dac-23ab-47db-8503-27c8366abb13' class='xr-section-summary' >Attributes: <span>(11)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'><dt><span>long_name :</span></dt><dd>4xDaily Air temperature at sigma level 995</dd><dt><span>units :</span></dt><dd>degK</dd><dt><span>precision :</span></dt><dd>2</dd><dt><span>GRIB_id :</span></dt><dd>11</dd><dt><span>GRIB_name :</span></dt><dd>TMP</dd><dt><span>var_desc :</span></dt><dd>Air temperature</dd><dt><span>dataset :</span></dt><dd>NMC Reanalysis</dd><dt><span>level_desc :</span></dt><dd>Surface</dd><dt><span>statistic :</span></dt><dd>Individual Obs</dd><dt><span>parent_stat :</span></dt><dd>Other</dd><dt><span>actual_range :</span></dt><dd>[185.16 322.1 ]</dd></dl></div></li></ul></div></div>"
],
"text/plain": [
"<xarray.DataArray 'air' (time: 2920, degrees_north: 4, degrees_east: 4)>\n",
Expand Down Expand Up @@ -2283,7 +2276,7 @@
"Attributes: (11)"
]
},
"execution_count": 41,
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -2295,18 +2288,6 @@
"da_air.sel(lat=target_lat, lon=target_lon, method=\"nearest\") # -- orthogonal indexing"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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",
"\n",
"da.sel(lat=target_lat, lon=target_lon, method=\"nearest\") # -- orthogonal indexing"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 9ff571a

Please sign in to comment.