Skip to content

Commit 2ab0666

Browse files
EricKeenanmathause
andauthored
Adding vectorized indexing docs (#4711)
Co-authored-by: Mathias Hauser <[email protected]> Co-authored-by: Mathias Hauser <[email protected]>
1 parent 8bf415a commit 2ab0666

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

doc/indexing.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,22 @@ These methods may also be applied to ``Dataset`` objects
395395
ds = da.to_dataset(name="bar")
396396
ds.isel(x=xr.DataArray([0, 1, 2], dims=["points"]))
397397
398+
Vectorized indexing may be used to extract information from the nearest
399+
grid cells of interest, for example, the nearest climate model grid cells
400+
to a collection specified weather station latitudes and longitudes.
401+
402+
.. ipython:: python
403+
404+
ds = xr.tutorial.open_dataset("air_temperature")
405+
406+
# Define target latitude and longitude (where weather stations might be)
407+
target_lon = xr.DataArray([200, 201, 202, 205], dims="points")
408+
target_lat = xr.DataArray([31, 41, 42, 42], dims="points")
409+
410+
# Retrieve data at the grid cells nearest to the target latitudes and longitudes
411+
da = ds["air"].sel(lon=target_lon, lat=target_lat, method="nearest")
412+
da
413+
398414
.. tip::
399415

400416
If you are lazily loading your data from disk, not every form of vectorized

xarray/core/dataarray.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,26 @@ def isel(
10941094
--------
10951095
Dataset.isel
10961096
DataArray.sel
1097+
1098+
Examples
1099+
--------
1100+
>>> da = xr.DataArray(np.arange(25).reshape(5, 5), dims=("x", "y"))
1101+
>>> da
1102+
<xarray.DataArray (x: 5, y: 5)>
1103+
array([[ 0, 1, 2, 3, 4],
1104+
[ 5, 6, 7, 8, 9],
1105+
[10, 11, 12, 13, 14],
1106+
[15, 16, 17, 18, 19],
1107+
[20, 21, 22, 23, 24]])
1108+
Dimensions without coordinates: x, y
1109+
1110+
>>> tgt_x = xr.DataArray(np.arange(0, 5), dims="points")
1111+
>>> tgt_y = xr.DataArray(np.arange(0, 5), dims="points")
1112+
>>> da = da.isel(x=tgt_x, y=tgt_y)
1113+
>>> da
1114+
<xarray.DataArray (points: 5)>
1115+
array([ 0, 6, 12, 18, 24])
1116+
Dimensions without coordinates: points
10971117
"""
10981118

10991119
indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "isel")
@@ -1202,6 +1222,34 @@ def sel(
12021222
Dataset.sel
12031223
DataArray.isel
12041224
1225+
Examples
1226+
--------
1227+
>>> da = xr.DataArray(
1228+
... np.arange(25).reshape(5, 5),
1229+
... coords={"x": np.arange(5), "y": np.arange(5)},
1230+
... dims=("x", "y"),
1231+
... )
1232+
>>> da
1233+
<xarray.DataArray (x: 5, y: 5)>
1234+
array([[ 0, 1, 2, 3, 4],
1235+
[ 5, 6, 7, 8, 9],
1236+
[10, 11, 12, 13, 14],
1237+
[15, 16, 17, 18, 19],
1238+
[20, 21, 22, 23, 24]])
1239+
Coordinates:
1240+
* x (x) int64 0 1 2 3 4
1241+
* y (y) int64 0 1 2 3 4
1242+
1243+
>>> tgt_x = xr.DataArray(np.linspace(0, 4, num=5), dims="points")
1244+
>>> tgt_y = xr.DataArray(np.linspace(0, 4, num=5), dims="points")
1245+
>>> da = da.sel(x=tgt_x, y=tgt_y, method="nearest")
1246+
>>> da
1247+
<xarray.DataArray (points: 5)>
1248+
array([ 0, 6, 12, 18, 24])
1249+
Coordinates:
1250+
x (points) int64 0 1 2 3 4
1251+
y (points) int64 0 1 2 3 4
1252+
Dimensions without coordinates: points
12051253
"""
12061254
ds = self._to_temp_dataset().sel(
12071255
indexers=indexers,

0 commit comments

Comments
 (0)