Skip to content

Commit

Permalink
Adding vectorized indexing docs (#4711)
Browse files Browse the repository at this point in the history
Co-authored-by: Mathias Hauser <[email protected]>
Co-authored-by: Mathias Hauser <[email protected]>
  • Loading branch information
3 people authored Feb 16, 2021
1 parent 8bf415a commit 2ab0666
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
16 changes: 16 additions & 0 deletions doc/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,22 @@ These methods may also be applied to ``Dataset`` objects
ds = da.to_dataset(name="bar")
ds.isel(x=xr.DataArray([0, 1, 2], dims=["points"]))
Vectorized indexing may be used to extract information from the nearest
grid cells of interest, for example, the nearest climate model grid cells
to a collection specified weather station latitudes and longitudes.

.. ipython:: python
ds = xr.tutorial.open_dataset("air_temperature")
# Define target latitude and longitude (where weather stations might be)
target_lon = xr.DataArray([200, 201, 202, 205], dims="points")
target_lat = xr.DataArray([31, 41, 42, 42], dims="points")
# Retrieve data at the grid cells nearest to the target latitudes and longitudes
da = ds["air"].sel(lon=target_lon, lat=target_lat, method="nearest")
da
.. tip::

If you are lazily loading your data from disk, not every form of vectorized
Expand Down
48 changes: 48 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,26 @@ def isel(
--------
Dataset.isel
DataArray.sel
Examples
--------
>>> da = xr.DataArray(np.arange(25).reshape(5, 5), dims=("x", "y"))
>>> da
<xarray.DataArray (x: 5, y: 5)>
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
Dimensions without coordinates: x, y
>>> tgt_x = xr.DataArray(np.arange(0, 5), dims="points")
>>> tgt_y = xr.DataArray(np.arange(0, 5), dims="points")
>>> da = da.isel(x=tgt_x, y=tgt_y)
>>> da
<xarray.DataArray (points: 5)>
array([ 0, 6, 12, 18, 24])
Dimensions without coordinates: points
"""

indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "isel")
Expand Down Expand Up @@ -1202,6 +1222,34 @@ def sel(
Dataset.sel
DataArray.isel
Examples
--------
>>> da = xr.DataArray(
... np.arange(25).reshape(5, 5),
... coords={"x": np.arange(5), "y": np.arange(5)},
... dims=("x", "y"),
... )
>>> da
<xarray.DataArray (x: 5, y: 5)>
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
Coordinates:
* x (x) int64 0 1 2 3 4
* y (y) int64 0 1 2 3 4
>>> tgt_x = xr.DataArray(np.linspace(0, 4, num=5), dims="points")
>>> tgt_y = xr.DataArray(np.linspace(0, 4, num=5), dims="points")
>>> da = da.sel(x=tgt_x, y=tgt_y, method="nearest")
>>> da
<xarray.DataArray (points: 5)>
array([ 0, 6, 12, 18, 24])
Coordinates:
x (points) int64 0 1 2 3 4
y (points) int64 0 1 2 3 4
Dimensions without coordinates: points
"""
ds = self._to_temp_dataset().sel(
indexers=indexers,
Expand Down

0 comments on commit 2ab0666

Please sign in to comment.