You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/indexing.rst
+72-52Lines changed: 72 additions & 52 deletions
Original file line number
Diff line number
Diff line change
@@ -11,19 +11,19 @@ Indexing and selecting data
11
11
import xarray as xr
12
12
np.random.seed(123456)
13
13
14
+
xarray offers extremely flexible indexing routines that combine the best
15
+
features of NumPy and pandas for data selection.
14
16
15
-
The point of xarray is to introduce a numpy-ndarray-like multidimensional array object into a powerful pandas's flexible data handling scheme.
16
-
We provide several (say, numpy-like, pandas-like, and more advanced type) indexing functionalities.
17
-
18
-
The most basic way to access each element of xarray's multi-dimensional
19
-
object is to use Python ``[obj]`` syntax, such as ``array[i, j]``, where ``i`` and ``j`` are both integers.
20
-
As xarray objects can store coordinates corresponding to each dimension of the
17
+
The most basic way to access elements of a :py:class:`~xarray.DataArray`
18
+
object is to use Python's ``[]`` syntax, such as ``array[i, j]``, where
19
+
``i`` and ``j`` are both integers.
20
+
As xarray objects can store coordinates corresponding to each dimension of an
21
21
array, label-based indexing similar to ``pandas.DataFrame.loc`` is also possible.
22
22
In label-based indexing, the element position ``i`` is automatically
23
23
looked-up from the coordinate values.
24
24
25
-
Dimensions of xarray object have names and you can also lookup the dimensions
26
-
by name, instead of remembering the positional ordering of dimensions by yourself.
25
+
Dimensions of xarray objects have names, so you can also lookup the dimensions
26
+
by name, instead of remembering their positional order.
27
27
28
28
Thus in total, xarray supports four different kinds of indexing, as described
29
29
below and summarized in this table:
@@ -271,27 +271,27 @@ elements that are fully masked:
271
271
Vectorized Indexing
272
272
-------------------
273
273
274
-
xarray supports many types of indexing with a `vectorized` manner.
274
+
Like numpy and pandas, xarray supports indexing many array elements at once in a
275
+
`vectorized` manner.
275
276
276
-
If you provide an integer, slice, or unlabeled array (array without dimension names, such as ``np.ndarray``, ``list``, but not :py:meth:`~xarray.DataArray` or :py:meth:`~xarray.Variable`)
277
-
our indexing is basically orthogonal.
278
-
For example,
279
-
if you pass multiple integer sequences to an array, they work independently
280
-
along each dimension (similar to the way vector subscripts work in fortran).
277
+
If you only provide integers, slices, or unlabeled arrays (array without
278
+
dimension names, such as ``np.ndarray``, ``list``, but not
279
+
:py:meth:`~xarray.DataArray` or :py:meth:`~xarray.Variable`) indexing can be
280
+
understand as orthogonally. Each indexer component selects independently along
281
+
the corresponding dimension, similar to how vector indexing works in Fortran or
282
+
MATLAB, or after using the :py:func:`numpy.xi_` helper:
281
283
282
284
.. ipython:: python
283
285
284
286
da = xr.DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y'],
@@ -648,28 +656,40 @@ dimensions or use the ellipsis in the ``loc`` specifier, e.g. in the example
648
656
above, ``mda.loc[{'one': 'a', 'two': 0}, :]`` or ``mda.loc[('a', 0), ...]``.
649
657
650
658
651
-
.. _xarray_indexing_rules:
659
+
.. _indexing.rules:
652
660
653
-
xarray indexing rules
654
-
---------------------
661
+
Indexing rules
662
+
--------------
655
663
656
-
The detailed indexing scheme in xarray is as follows.
657
-
(Note that it is for the explanation purpose and the actual implementation is differ.)
664
+
Here we describe the full rules xarray uses for vectorized indexing. Note that
665
+
this is for the purposes of explanation: for the sake of efficiency and to
666
+
support various backends, the actual implementation is different.
658
667
659
-
0. (Only for label based indexing.) Look up positional indexes along each dimension based on :py:class:`pandas.Index`.
668
+
0. (Only for label based indexing.) Look up positional indexes along each
669
+
dimension from the corresponding :py:class:`pandas.Index`.
660
670
661
-
1. ``slice`` is converted to an array, such that ``np.arange(*slice.indices(...))``.
671
+
1. A full slice object ``:`` is inserted for each dimension without an indexer.
662
672
663
-
2. Assume dimension names of array indexers without dimension, such as ``np.ndarray`` and ``list``, from the dimensions to be indexed along. For example, ``v.isel(x=[0, 1])`` is understood as ``v.isel(x=xr.DataArray([0, 1], dims=['x']))``.
673
+
2. ``slice`` objects are converted into arrays, given by
674
+
``np.arange(*slice.indices(...))``.
664
675
665
-
3. Broadcast all the indexers based on their dimension names (see :ref:`compute.broadcasting` for our name-based broadcasting).
676
+
3. Assume dimension names for array indexers without dimensions, such as
677
+
``np.ndarray`` and ``list``, from the dimensions to be indexed along.
678
+
For example, ``v.isel(x=[0, 1])`` is understood as
679
+
``v.isel(x=xr.DataArray([0, 1], dims=['x']))``.
666
680
667
-
4. Index the object by the broadcasted indexers.
681
+
4. For each variable in a ``Dataset`` or ``DataArray`` (the array and its
682
+
coordinates):
668
683
669
-
5. If an indexer-DataArray has coordinates, attached them to the indexed object.
684
+
a. Broadcast all relevant indexers based on their dimension names
685
+
(see :ref:`compute.broadcasting` for full details).
670
686
671
-
.. note::
687
+
b. Index the underling array by the broadcast indexers, using NumPy's
688
+
advanced indexing rules.
689
+
690
+
5. If any indexer DataArray has coordinates and no coordinate with the
691
+
same name exists, attach them to the indexed object.
672
692
673
-
+ There should not be a conflict between the coordinates of indexer- and indexed- DataArrays. In v.0.10.0, xarray raises ``FutureWarning`` if there is such a conflict, but in the next major release, it will raise an Error.
693
+
.. note::
674
694
675
-
+ Only 1-dimensional boolean array can be used as an indexer.
695
+
Only 1-dimensional boolean arrays can be used as indexers.
0 commit comments