Indexing and selecting data¶
Similarly to pandas objects, xarray objects support both integer and label based lookups along each dimension. However, xarray objects also have named dimensions, so you can optionally use dimension names instead of relying on the positional ordering of dimensions.
Thus in total, xarray supports four different kinds of indexing, as described below and summarized in this table:
Dimension lookup | Index lookup | DataArray syntax |
Dataset syntax |
---|---|---|---|
Positional | By integer | arr[:, 0] |
not available |
Positional | By label | arr.loc[:, 'IA'] |
not available |
By name | By integer | arr.isel(space=0) or arr[dict(space=0)] |
ds.isel(space=0) or ds[dict(space=0)] |
By name | By label | arr.sel(space='IA') or arr.loc[dict(space='IA')] |
ds.sel(space='IA') or ds.loc[dict(space='IA')] |
Positional indexing¶
Indexing a DataArray
directly works (mostly) just like it
does for numpy arrays, except that the returned object is always another
DataArray:
In [1]: arr = xr.DataArray(np.random.rand(4, 3),
...: [('time', pd.date_range('2000-01-01', periods=4)),
...: ('space', ['IA', 'IL', 'IN'])])
...:
In [2]: arr[:2]
Out[2]:
<xarray.DataArray (time: 2, space: 3)>
array([[ 0.12697 , 0.966718, 0.260476],
[ 0.897237, 0.37675 , 0.336222]])
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-02
* space (space) <U2 'IA' 'IL' 'IN'
In [3]: arr[0, 0]