Indexing and selecting data

xarray offers extremely flexible indexing routines that combine the bestfeatures of NumPy and pandas for data selection.

The most basic way to access elements of a DataArrayobject is to use Python’s [] syntax, such as array[i, j], wherei and j are both integers.As xarray objects can store coordinates corresponding to each dimension of anarray, label-based indexing similar to pandas.DataFrame.loc is also possible.In label-based indexing, the element position i is automaticallylooked-up from the coordinate values.

Dimensions of xarray objects have names, so you can also lookup the dimensionsby name, instead of remembering their positional order.

Thus in total, xarray supports four different kinds of indexing, as describedbelow and summarized in this table:

Dimension lookupIndex lookupDataArray syntaxDataset syntax
PositionalBy integerda[:, 0]not available
PositionalBy labelda.loc[:, 'IA']not available
By nameBy integerda.isel(space=0) or da[dict(space=0)]ds.isel(space=0) or ds[dict(space=0)]
By nameBy labelda.sel(space='IA') or da.loc[dict(space='IA')]ds.sel(space='IA') or ds.loc[dict(space='IA')]

More advanced indexing is also possible for all the methods bysupplying DataArray objects as indexer.See Vectorized Indexing for the details.

Positional indexing

Indexing a DataArray directly works (mostly) just like itdoes for numpy arrays, except that the returned object is always anotherDataArray:

  1. In [1]: da = xr.DataArray(np.random.rand(4, 3),
  2. ...: [('time', pd.date_range('2000-01-01', periods=4)),
  3. ...: ('space', ['IA', 'IL', 'IN'])])
  4. ...:
  5.  
  6. In [2]: da[:2]
  7. Out[2]:
  8. <xarray.DataArray (time: 2, space: 3)>
  9. array([[0.12697 , 0.966718, 0.260476],
  10. [0.897237, 0.37675 , 0.336222]])
  11. Coordinates:
  12. * time (time) datetime64[ns] 2000-01-01 2000-01-02
  13. * space (space) <U2 'IA' 'IL' 'IN'
  14.  
  15. In [3]: da[0, 0]
  16. Out[3]:
  17. <xarray.DataArray ()>
  18. array(0.12697)
  19. Coordinates:
  20. time datetime64[ns] 2000-01-01
  21. space <U2 'IA'
  22.  
  23. In [4]: da[:, [2, 1]]
  24. Out[4]:
  25. <xarray.DataArray (time: 4, space: 2)>
  26. array([[0.260476, 0.966718],
  27. [0.336222, 0.37675 ],
  28. [0.123102, 0.840255],
  29. [0.447997, 0.373012]])
  30. Coordinates:
  31. * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  32. * space (space) <U2 'IN' 'IL'

Attributes are persisted in all indexing operations.

Warning

Positional indexing deviates from the NumPy when indexing with multiplearrays like da[[0, 1], [0, 1]], as described inVectorized Indexing.

xarray also supports label-based indexing, just like pandas. Becausewe use a pandas.Index under the hood, label based indexing is veryfast. To do label based indexing, use the loc attribute:

  1. In [5]: da.loc['2000-01-01':'2000-01-02', 'IA']
  2. Out[5]:
  3. <xarray.DataArray (time: 2)>
  4. array([0.12697 , 0.897237])
  5. Coordinates:
  6. * time (time) datetime64[ns] 2000-01-01 2000-01-02
  7. space <U2 'IA'

In this example, the selected is a subpart of the arrayin the range ‘2000-01-01’:‘2000-01-02’ along the first coordinate time_and with ‘IA’ value from the second coordinate _space.

You can perform any of the label indexing operations supported by pandas,including indexing with individual, slices and arrays of labels, as well asindexing with boolean arrays. Like pandas, label based indexing in xarray isinclusive of both the start and stop bounds.

Setting values with label based indexing is also supported:

  1. In [6]: da.loc['2000-01-01', ['IL', 'IN']] = -10
  2.  
  3. In [7]: da
  4. Out[7]:
  5. <xarray.DataArray (time: 4, space: 3)>
  6. array([[ 0.12697 , -10. , -10. ],
  7. [ 0.897237, 0.37675 , 0.336222],
  8. [ 0.451376, 0.840255, 0.123102],
  9. [ 0.543026, 0.373012, 0.447997]])
  10. Coordinates:
  11. * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  12. * space (space) <U2 'IA' 'IL' 'IN'

Indexing with dimension names

With the dimension names, we do not have to rely on dimension order and canuse them explicitly to slice data. There are two ways to do this:

  • Use a dictionary as the argument for array positional or label based arrayindexing:
  1. # index by integer array indicesIn [8]: da[dict(space=0, time=slice(None, 2))]Out[8]:<xarray.DataArray (time: 2)>array([0.12697 , 0.897237])Coordinates: time (time) datetime64[ns] 2000-01-01 2000-01-02 space <U2 'IA'# index by dimension coordinate labelsIn [9]: da.loc[dict(time=slice('2000-01-01', '2000-01-02'))]Out[9]:<xarray.DataArray (time: 2, space: 3)>array([[ 0.12697 , -10. , -10. ], [ 0.897237, 0.37675 , 0.336222]])Coordinates:
  2. time (time) datetime64[ns] 2000-01-01 2000-01-02 * space (space) <U2 'IA' 'IL' 'IN'
  1. # index by integer array indicesIn [10]: da.isel(space=0, time=slice(None, 2))Out[10]:<xarray.DataArray (time: 2)>array([0.12697 , 0.897237])Coordinates: time (time) datetime64[ns] 2000-01-01 2000-01-02 space <U2 'IA'# index by dimension coordinate labelsIn [11]: da.sel(time=slice('2000-01-01', '2000-01-02'))Out[11]:<xarray.DataArray (time: 2, space: 3)>array([[ 0.12697 , -10. , -10. ], [ 0.897237, 0.37675 , 0.336222]])Coordinates:
  2. time (time) datetime64[ns] 2000-01-01 2000-01-02 * space (space) <U2 'IA' 'IL' 'IN'

The arguments to these methods can be any objects that could index the arrayalong the dimension given by the keyword, e.g., labels for an individual value,Python slice() objects or 1-dimensional arrays.

Note

We would love to be able to do indexing with labeled dimension names insidebrackets, but unfortunately, Python does yet not support indexing withkeyword arguments like da[space=0]

Nearest neighbor lookups

The label based selection methods sel(),reindex() and reindex_like() allsupport method and tolerance keyword argument. The method parameter allows forenabling nearest neighbor (inexact) lookups by use of the methods 'pad','backfill' or 'nearest':

  1. In [12]: da = xr.DataArray([1, 2, 3], [('x', [0, 1, 2])])
  2.  
  3. In [13]: da.sel(x=[1.1, 1.9], method='nearest')
  4. Out[13]:
  5. <xarray.DataArray (x: 2)>
  6. array([2, 3])
  7. Coordinates:
  8. * x (x) int64 1 2
  9.  
  10. In [14]: da.sel(x=0.1, method='backfill')
  11. Out[14]:
  12. <xarray.DataArray ()>
  13. array(2)
  14. Coordinates:
  15. x int64 1
  16.  
  17. In [15]: da.reindex(x=[0.5, 1, 1.5, 2, 2.5], method='pad')
  18. Out[15]:
  19. <xarray.DataArray (x: 5)>
  20. array([1, 2, 2, 3, 3])
  21. Coordinates:
  22. * x (x) float64 0.5 1.0 1.5 2.0 2.5

Tolerance limits the maximum distance for valid matches with an inexact lookup:

  1. In [16]: da.reindex(x=[1.1, 1.5], method='nearest', tolerance=0.2)
  2. Out[16]:
  3. <xarray.DataArray (x: 2)>
  4. array([ 2., nan])
  5. Coordinates:
  6. * x (x) float64 1.1 1.5

The method parameter is not yet supported if any of the argumentsto .sel() is a slice object:

  1. In [17]: da.sel(x=slice(1, 3), method='nearest')
  2. NotImplementedError

However, you don’t need to use method to do inexact slicing. Slicingalready returns all values inside the range (inclusive), as long as the indexlabels are monotonic increasing:

  1. In [18]: da.sel(x=slice(0.9, 3.1))
  2. Out[18]:
  3. <xarray.DataArray (x: 2)>
  4. array([2, 3])
  5. Coordinates:
  6. * x (x) int64 1 2

Indexing axes with monotonic decreasing labels also works, as long as theslice or .loc arguments are also decreasing:

  1. In [19]: reversed_da = da[::-1]
  2.  
  3. In [20]: reversed_da.loc[3.1:0.9]
  4. Out[20]:
  5. <xarray.DataArray (x: 2)>
  6. array([3, 2])
  7. Coordinates:
  8. * x (x) int64 2 1

Note

If you want to interpolate along coordinates rather than looking up thenearest neighbors, use interp() andinterp_like().See interpolation for the details.

Dataset indexing

We can also use these methods to index all variables in a datasetsimultaneously, returning a new dataset:

  1. In [21]: da = xr.DataArray(np.random.rand(4, 3),
  2. ....: [('time', pd.date_range('2000-01-01', periods=4)),
  3. ....: ('space', ['IA', 'IL', 'IN'])])
  4. ....:
  5.  
  6. In [22]: ds = da.to_dataset(name='foo')
  7.  
  8. In [23]: ds.isel(space=[0], time=[0])
  9. Out[23]:
  10. <xarray.Dataset>
  11. Dimensions: (space: 1, time: 1)
  12. Coordinates:
  13. * time (time) datetime64[ns] 2000-01-01
  14. * space (space) <U2 'IA'
  15. Data variables:
  16. foo (time, space) float64 0.1294
  17.  
  18. In [24]: ds.sel(time='2000-01-01')
  19. Out[24]:
  20. <xarray.Dataset>
  21. Dimensions: (space: 3)
  22. Coordinates:
  23. time datetime64[ns] 2000-01-01
  24. * space (space) <U2 'IA' 'IL' 'IN'
  25. Data variables:
  26. foo (space) float64 0.1294 0.8599 0.8204

Positional indexing on a dataset is not supported because the ordering ofdimensions in a dataset is somewhat ambiguous (it can vary between differentarrays). However, you can do normal indexing with dimension names:

  1. In [25]: ds[dict(space=[0], time=[0])]
  2. Out[25]:
  3. <xarray.Dataset>
  4. Dimensions: (space: 1, time: 1)
  5. Coordinates:
  6. * time (time) datetime64[ns] 2000-01-01
  7. * space (space) <U2 'IA'
  8. Data variables:
  9. foo (time, space) float64 0.1294
  10.  
  11. In [26]: ds.loc[dict(time='2000-01-01')]
  12. Out[26]:
  13. <xarray.Dataset>
  14. Dimensions: (space: 3)
  15. Coordinates:
  16. time datetime64[ns] 2000-01-01
  17. * space (space) <U2 'IA' 'IL' 'IN'
  18. Data variables:
  19. foo (space) float64 0.1294 0.8599 0.8204

Using indexing to assign values to a subset of dataset (e.g.,ds[dict(space=0)] = 1) is not yet supported.

Dropping labels and dimensions

The drop() method returns a new object with the listedindex labels along a dimension dropped:

  1. In [27]: ds.drop(['IN', 'IL'], dim='space')
  2. Out[27]:
  3. <xarray.Dataset>
  4. Dimensions: (space: 1, time: 4)
  5. Coordinates:
  6. * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  7. * space (space) <U2 'IA'
  8. Data variables:
  9. foo (time, space) float64 0.1294 0.3521 0.5948 0.2355

drop is both a Dataset and DataArray method.

Use drop_dims() to drop a full dimension from a Dataset.Any variables with these dimensions are also dropped:

  1. In [28]: ds.drop_dims('time')
  2. Out[28]:
  3. <xarray.Dataset>
  4. Dimensions: (space: 3)
  5. Coordinates:
  6. * space (space) <U2 'IA' 'IL' 'IN'
  7. Data variables:
  8. *empty*

Masking with where

Indexing methods on xarray objects generally return a subset of the original data.However, it is sometimes useful to select an object with the same shape as theoriginal data, but with some elements masked. To do this type of selection inxarray, use where():

  1. In [29]: da = xr.DataArray(np.arange(16).reshape(4, 4), dims=['x', 'y'])
  2.  
  3. In [30]: da.where(da.x + da.y < 4)
  4. Out[30]:
  5. <xarray.DataArray (x: 4, y: 4)>
  6. array([[ 0., 1., 2., 3.],
  7. [ 4., 5., 6., nan],
  8. [ 8., 9., nan, nan],
  9. [12., nan, nan, nan]])
  10. Dimensions without coordinates: x, y

This is particularly useful for ragged indexing of multi-dimensional data,e.g., to apply a 2D mask to an image. Note that where follows all theusual xarray broadcasting and alignment rules for binary operations (e.g.,+) between the object being indexed and the condition, as described inComputation:

  1. In [31]: da.where(da.y < 2)
  2. Out[31]:
  3. <xarray.DataArray (x: 4, y: 4)>
  4. array([[ 0., 1., nan, nan],
  5. [ 4., 5., nan, nan],
  6. [ 8., 9., nan, nan],
  7. [12., 13., nan, nan]])
  8. Dimensions without coordinates: x, y

By default where maintains the original size of the data. For caseswhere the selected data size is much smaller than the original data,use of the option drop=True clips coordinateelements that are fully masked:

  1. In [32]: da.where(da.y < 2, drop=True)
  2. Out[32]:
  3. <xarray.DataArray (x: 4, y: 2)>
  4. array([[ 0., 1.],
  5. [ 4., 5.],
  6. [ 8., 9.],
  7. [12., 13.]])
  8. Dimensions without coordinates: x, y

Selecting values with isin

To check whether elements of an xarray object contain a single object, you cancompare with the equality operator == (e.g., arr == 3). To checkmultiple values, use isin():

  1. In [33]: da = xr.DataArray([1, 2, 3, 4, 5], dims=['x'])
  2.  
  3. In [34]: da.isin([2, 4])
  4. Out[34]:
  5. <xarray.DataArray (x: 5)>
  6. array([False, True, False, True, False])
  7. Dimensions without coordinates: x

isin() works particularly well withwhere() to support indexing by arrays that are notalready labels of an array:

  1. In [35]: lookup = xr.DataArray([-1, -2, -3, -4, -5], dims=['x'])
  2.  
  3. In [36]: da.where(lookup.isin([-2, -4]), drop=True)
  4. Out[36]:
  5. <xarray.DataArray (x: 2)>
  6. array([2., 4.])
  7. Dimensions without coordinates: x

However, some caution is in order: when done repeatedly, this type of indexingis significantly slower than using sel().

Vectorized Indexing

Like numpy and pandas, xarray supports indexing many array elements at once in avectorized manner.

If you only provide integers, slices, or unlabeled arrays (array withoutdimension names, such as np.ndarray, list, but notDataArray() or Variable()) indexing can beunderstood as orthogonally. Each indexer component selects independently alongthe corresponding dimension, similar to how vector indexing works in Fortran orMATLAB, or after using the numpy.ix_() helper:

  1. In [37]: da = xr.DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y'],
  2. ....: coords={'x': [0, 1, 2], 'y': ['a', 'b', 'c', 'd']})
  3. ....:
  4.  
  5. In [38]: da
  6. Out[38]:
  7. <xarray.DataArray (x: 3, y: 4)>
  8. array([[ 0, 1, 2, 3],
  9. [ 4, 5, 6, 7],
  10. [ 8, 9, 10, 11]])
  11. Coordinates:
  12. * x (x) int64 0 1 2
  13. * y (y) <U1 'a' 'b' 'c' 'd'
  14.  
  15. In [39]: da[[0, 1], [1, 1]]
  16. Out[39]:
  17. <xarray.DataArray (x: 2, y: 2)>
  18. array([[1, 1],
  19. [5, 5]])
  20. Coordinates:
  21. * x (x) int64 0 1
  22. * y (y) <U1 'b' 'b'

For more flexibility, you can supply DataArray() objectsas indexers.Dimensions on resultant arrays are given by the ordered union of the indexers’dimensions:

  1. In [40]: ind_x = xr.DataArray([0, 1], dims=['x'])
  2.  
  3. In [41]: ind_y = xr.DataArray([0, 1], dims=['y'])
  4.  
  5. In [42]: da[ind_x, ind_y] # orthogonal indexing
  6. Out[42]:
  7. <xarray.DataArray (x: 2, y: 2)>
  8. array([[0, 1],
  9. [4, 5]])
  10. Coordinates:
  11. * x (x) int64 0 1
  12. * y (y) <U1 'a' 'b'
  13.  
  14. In [43]: da[ind_x, ind_x] # vectorized indexing
  15. Out[43]:
  16. <xarray.DataArray (x: 2)>
  17. array([0, 5])
  18. Coordinates:
  19. * x (x) int64 0 1
  20. y (x) <U1 'a' 'b'

Slices or sequences/arrays without named-dimensions are treated as if they havethe same dimension which is indexed along:

  1. # Because [0, 1] is used to index along dimension 'x',
  2. # it is assumed to have dimension 'x'
  3. In [44]: da[[0, 1], ind_x]
  4. Out[44]:
  5. <xarray.DataArray (x: 2)>
  6. array([0, 5])
  7. Coordinates:
  8. * x (x) int64 0 1
  9. y (x) <U1 'a' 'b'

Furthermore, you can use multi-dimensional DataArray()as indexers, where the resultant array dimension is also determined byindexers’ dimension:

  1. In [45]: ind = xr.DataArray([[0, 1], [0, 1]], dims=['a', 'b'])
  2.  
  3. In [46]: da[ind]
  4. Out[46]:
  5. <xarray.DataArray (a: 2, b: 2, y: 4)>
  6. array([[[0, 1, 2, 3],
  7. [4, 5, 6, 7]],
  8.  
  9. [[0, 1, 2, 3],
  10. [4, 5, 6, 7]]])
  11. Coordinates:
  12. x (a, b) int64 0 1 0 1
  13. * y (y) <U1 'a' 'b' 'c' 'd'
  14. Dimensions without coordinates: a, b

Similar to how NumPy’s advanced indexing works, vectorizedindexing for xarray is based on ourbroadcasting rules.See Indexing rules for the complete specification.

Vectorized indexing also works with isel, loc, and sel:

  1. In [47]: ind = xr.DataArray([[0, 1], [0, 1]], dims=['a', 'b'])
  2.  
  3. In [48]: da.isel(y=ind) # same as da[:, ind]
  4. Out[48]:
  5. <xarray.DataArray (x: 3, a: 2, b: 2)>
  6. array([[[0, 1],
  7. [0, 1]],
  8.  
  9. [[4, 5],
  10. [4, 5]],
  11.  
  12. [[8, 9],
  13. [8, 9]]])
  14. Coordinates:
  15. * x (x) int64 0 1 2
  16. y (a, b) object 'a' 'b' 'a' 'b'
  17. Dimensions without coordinates: a, b
  18.  
  19. In [49]: ind = xr.DataArray([['a', 'b'], ['b', 'a']], dims=['a', 'b'])
  20.  
  21. In [50]: da.loc[:, ind] # same as da.sel(y=ind)
  22. Out[50]:
  23. <xarray.DataArray (x: 3, a: 2, b: 2)>
  24. array([[[0, 1],
  25. [1, 0]],
  26.  
  27. [[4, 5],
  28. [5, 4]],
  29.  
  30. [[8, 9],
  31. [9, 8]]])
  32. Coordinates:
  33. * x (x) int64 0 1 2
  34. y (a, b) object 'a' 'b' 'b' 'a'
  35. Dimensions without coordinates: a, b

These methods may also be applied to Dataset objects

  1. In [51]: ds = da.to_dataset(name='bar')
  2.  
  3. In [52]: ds.isel(x=xr.DataArray([0, 1, 2], dims=['points']))
  4. Out[52]:
  5. <xarray.Dataset>
  6. Dimensions: (points: 3, y: 4)
  7. Coordinates:
  8. x (points) int64 0 1 2
  9. * y (y) <U1 'a' 'b' 'c' 'd'
  10. Dimensions without coordinates: points
  11. Data variables:
  12. bar (points, y) int64 0 1 2 3 4 5 6 7 8 9 10 11

Tip

If you are lazily loading your data from disk, not every form of vectorizedindexing is supported (or if supported, may not be supported efficiently).You may find increased performance by loading your data into memory first,e.g., with load().

Note

Vectorized indexing is a new feature in v0.10.In older versions of xarray, dimensions of indexers are ignored.Dedicated methods for some advanced indexing use cases,isel_points and sel_points are now deprecated.See More advanced indexing for their alternative.

Note

If an indexer is a DataArray(), its coordinates should notconflict with the selected subpart of the target array (except for theexplicitly indexed dimensions with .loc/.sel).Otherwise, IndexError will be raised.

Assigning values with indexing

To select and assign values to a portion of a DataArray() youcan use indexing with .loc :

  1. In [53]: ds = xr.tutorial.open_dataset('air_temperature')
  2.  
  3. #add an empty 2D dataarray
  4. In [54]: ds['empty']= xr.full_like(ds.air.mean('time'),fill_value=0)
  5.  
  6. #modify one grid point using loc()
  7. In [55]: ds['empty'].loc[dict(lon=260, lat=30)] = 100
  8.  
  9. #modify a 2D region using loc()
  10. In [56]: lc = ds.coords['lon']
  11.  
  12. In [57]: la = ds.coords['lat']
  13.  
  14. In [58]: ds['empty'].loc[dict(lon=lc[(lc>220)&(lc<260)], lat=la[(la>20)&(la<60)])] = 100

or where():

  1. #modify one grid point using xr.where()
  2. In [59]: ds['empty'] = xr.where((ds.coords['lat']==20)&(ds.coords['lon']==260), 100, ds['empty'])
  3.  
  4. #or modify a 2D region using xr.where()
  5. In [60]: mask = (ds.coords['lat']>20)&(ds.coords['lat']<60)&(ds.coords['lon']>220)&(ds.coords['lon']<260)
  6.  
  7. In [61]: ds['empty'] = xr.where(mask, 100, ds['empty'])

Vectorized indexing can also be used to assign values to xarray object.

  1. In [62]: da = xr.DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y'],
  2. ....: coords={'x': [0, 1, 2], 'y': ['a', 'b', 'c', 'd']})
  3. ....:
  4.  
  5. In [63]: da
  6. Out[63]:
  7. <xarray.DataArray (x: 3, y: 4)>
  8. array([[ 0, 1, 2, 3],
  9. [ 4, 5, 6, 7],
  10. [ 8, 9, 10, 11]])
  11. Coordinates:
  12. * x (x) int64 0 1 2
  13. * y (y) <U1 'a' 'b' 'c' 'd'
  14.  
  15. In [64]: da[0] = -1 # assignment with broadcasting
  16.  
  17. In [65]: da
  18. Out[65]:
  19. <xarray.DataArray (x: 3, y: 4)>
  20. array([[-1, -1, -1, -1],
  21. [ 4, 5, 6, 7],
  22. [ 8, 9, 10, 11]])
  23. Coordinates:
  24. * x (x) int64 0 1 2
  25. * y (y) <U1 'a' 'b' 'c' 'd'
  26.  
  27. In [66]: ind_x = xr.DataArray([0, 1], dims=['x'])
  28.  
  29. In [67]: ind_y = xr.DataArray([0, 1], dims=['y'])
  30.  
  31. In [68]: da[ind_x, ind_y] = -2 # assign -2 to (ix, iy) = (0, 0) and (1, 1)
  32.  
  33. In [69]: da
  34. Out[69]:
  35. <xarray.DataArray (x: 3, y: 4)>
  36. array([[-2, -2, -1, -1],
  37. [-2, -2, 6, 7],
  38. [ 8, 9, 10, 11]])
  39. Coordinates:
  40. * x (x) int64 0 1 2
  41. * y (y) <U1 'a' 'b' 'c' 'd'
  42.  
  43. In [70]: da[ind_x, ind_y] += 100 # increment is also possible
  44.  
  45. In [71]: da
  46. Out[71]:
  47. <xarray.DataArray (x: 3, y: 4)>
  48. array([[98, 98, -1, -1],
  49. [98, 98, 6, 7],
  50. [ 8, 9, 10, 11]])
  51. Coordinates:
  52. * x (x) int64 0 1 2
  53. * y (y) <U1 'a' 'b' 'c' 'd'

Like numpy.ndarray, value assignment sometimes works differently from what one may expect.

  1. In [72]: da = xr.DataArray([0, 1, 2, 3], dims=['x'])
  2.  
  3. In [73]: ind = xr.DataArray([0, 0, 0], dims=['x'])
  4.  
  5. In [74]: da[ind] -= 1
  6.  
  7. In [75]: da
  8. Out[75]:
  9. <xarray.DataArray (x: 4)>
  10. array([-1, 1, 2, 3])
  11. Dimensions without coordinates: x

Where the 0th element will be subtracted 1 only once.This is because v[0] = v[0] - 1 is called three times, rather thanv[0] = v[0] - 1 - 1 - 1.See Assigning values to indexed arrays for the details.

Note

Dask array does not support value assignment(see Parallel computing with Dask for the details).

Note

Coordinates in both the left- and right-hand-side arrays should notconflict with each other.Otherwise, IndexError will be raised.

Warning

Do not try to assign values when using any of the indexing methods iselor sel:

  1. # DO NOT do this
  2. da.isel(space=0) = 0

Assigning values with the chained indexing using .sel or .isel fails silently.

  1. In [76]: da = xr.DataArray([0, 1, 2, 3], dims=['x'])
  2.  
  3. # DO NOT do this
  4. In [77]: da.isel(x=[0, 1, 2])[1] = -1
  5.  
  6. In [78]: da
  7. Out[78]:
  8. <xarray.DataArray (x: 4)>
  9. array([0, 1, 2, 3])
  10. Dimensions without coordinates: x

More advanced indexing

The use of DataArray() objects as indexers enables veryflexible indexing. The following is an example of the pointwise indexing:

  1. In [79]: da = xr.DataArray(np.arange(56).reshape((7, 8)), dims=['x', 'y'])
  2.  
  3. In [80]: da
  4. Out[80]:
  5. <xarray.DataArray (x: 7, y: 8)>
  6. array([[ 0, 1, 2, 3, 4, 5, 6, 7],
  7. [ 8, 9, 10, 11, 12, 13, 14, 15],
  8. [16, 17, 18, 19, 20, 21, 22, 23],
  9. [24, 25, 26, 27, 28, 29, 30, 31],
  10. [32, 33, 34, 35, 36, 37, 38, 39],
  11. [40, 41, 42, 43, 44, 45, 46, 47],
  12. [48, 49, 50, 51, 52, 53, 54, 55]])
  13. Dimensions without coordinates: x, y
  14.  
  15. In [81]: da.isel(x=xr.DataArray([0, 1, 6], dims='z'),
  16. ....: y=xr.DataArray([0, 1, 0], dims='z'))
  17. ....:
  18. Out[81]:
  19. <xarray.DataArray (z: 3)>
  20. array([ 0, 9, 48])
  21. Dimensions without coordinates: z

where three elements at (ix, iy) = ((0, 0), (1, 1), (6, 0)) are selectedand mapped along a new dimension z.

If you want to add a coordinate to the new dimension z,you can supply a DataArray with a coordinate,

  1. In [82]: da.isel(x=xr.DataArray([0, 1, 6], dims='z',
  2. ....: coords={'z': ['a', 'b', 'c']}),
  3. ....: y=xr.DataArray([0, 1, 0], dims='z'))
  4. ....:
  5. Out[82]:
  6. <xarray.DataArray (z: 3)>
  7. array([ 0, 9, 48])
  8. Coordinates:
  9. * z (z) <U1 'a' 'b' 'c'

Analogously, label-based pointwise-indexing is also possible by the .selmethod:

  1. In [83]: da = xr.DataArray(np.random.rand(4, 3),
  2. ....: [('time', pd.date_range('2000-01-01', periods=4)),
  3. ....: ('space', ['IA', 'IL', 'IN'])])
  4. ....:
  5.  
  6. In [84]: times = xr.DataArray(pd.to_datetime(['2000-01-03', '2000-01-02', '2000-01-01']),
  7. ....: dims='new_time')
  8. ....:
  9.  
  10. In [85]: da.sel(space=xr.DataArray(['IA', 'IL', 'IN'], dims=['new_time']),
  11. ....: time=times)
  12. ....:
  13. Out[85]:
  14. <xarray.DataArray (new_time: 3)>
  15. array([0.91954 , 0.340445, 0.590426])
  16. Coordinates:
  17. time (new_time) datetime64[ns] 2000-01-03 2000-01-02 2000-01-01
  18. space (new_time) <U2 'IA' 'IL' 'IN'
  19. * new_time (new_time) datetime64[ns] 2000-01-03 2000-01-02 2000-01-01

Align and reindex

xarray’s reindex, reindexlike and align impose a DataArray orDataset onto a new set of coordinates corresponding to dimensions. Theoriginal values are subset to the index labels still found in the new labels,and values corresponding to new labels not found in the original object arein-filled with _NaN.

xarray operations that combine multiple objects generally automatically aligntheir arguments to share the same indexes. However, manual alignment can beuseful for greater control and for increased performance.

To reindex a particular dimension, use reindex():

  1. In [86]: da.reindex(space=['IA', 'CA'])
  2. Out[86]:
  3. <xarray.DataArray (time: 4, space: 2)>
  4. array([[0.574012, nan],
  5. [0.24535 , nan],
  6. [0.91954 , nan],
  7. [0.753569, nan]])
  8. Coordinates:
  9. * space (space) object 'IA' 'CA'
  10. * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04

The reindex_like() method is a useful shortcut.To demonstrate, we will make a subset DataArray with new values:

  1. In [87]: foo = da.rename('foo')
  2.  
  3. In [88]: baz = (10 * da[:2, :2]).rename('baz')
  4.  
  5. In [89]: baz
  6. Out[89]:
  7. <xarray.DataArray 'baz' (time: 2, space: 2)>
  8. array([[5.740118, 0.6127 ],
  9. [2.453498, 3.404449]])
  10. Coordinates:
  11. * time (time) datetime64[ns] 2000-01-01 2000-01-02
  12. * space (space) <U2 'IA' 'IL'

Reindexing foo with baz selects out the first two values along eachdimension:

  1. In [90]: foo.reindex_like(baz)
  2. Out[90]:
  3. <xarray.DataArray 'foo' (time: 2, space: 2)>
  4. array([[0.574012, 0.06127 ],
  5. [0.24535 , 0.340445]])
  6. Coordinates:
  7. * time (time) datetime64[ns] 2000-01-01 2000-01-02
  8. * space (space) object 'IA' 'IL'

The opposite operation asks us to reindex to a larger shape, so we fill inthe missing values with NaN:

  1. In [91]: baz.reindex_like(foo)
  2. Out[91]:
  3. <xarray.DataArray 'baz' (time: 4, space: 3)>
  4. array([[5.740118, 0.6127 , nan],
  5. [2.453498, 3.404449, nan],
  6. [ nan, nan, nan],
  7. [ nan, nan, nan]])
  8. Coordinates:
  9. * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  10. * space (space) object 'IA' 'IL' 'IN'

The align() function lets us perform more flexible database-like'inner', 'outer', 'left' and 'right' joins:

  1. In [92]: xr.align(foo, baz, join='inner')
  2. Out[92]:
  3. (<xarray.DataArray 'foo' (time: 2, space: 2)>
  4. array([[0.574012, 0.06127 ],
  5. [0.24535 , 0.340445]])
  6. Coordinates:
  7. * time (time) datetime64[ns] 2000-01-01 2000-01-02
  8. * space (space) object 'IA' 'IL',
  9. <xarray.DataArray 'baz' (time: 2, space: 2)>
  10. array([[5.740118, 0.6127 ],
  11. [2.453498, 3.404449]])
  12. Coordinates:
  13. * time (time) datetime64[ns] 2000-01-01 2000-01-02
  14. * space (space) object 'IA' 'IL')
  15.  
  16. In [93]: xr.align(foo, baz, join='outer')
  17. Out[93]:
  18. (<xarray.DataArray 'foo' (time: 4, space: 3)>
  19. array([[0.574012, 0.06127 , 0.590426],
  20. [0.24535 , 0.340445, 0.984729],
  21. [0.91954 , 0.037772, 0.861549],
  22. [0.753569, 0.405179, 0.343526]])
  23. Coordinates:
  24. * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  25. * space (space) object 'IA' 'IL' 'IN',
  26. <xarray.DataArray 'baz' (time: 4, space: 3)>
  27. array([[5.740118, 0.6127 , nan],
  28. [2.453498, 3.404449, nan],
  29. [ nan, nan, nan],
  30. [ nan, nan, nan]])
  31. Coordinates:
  32. * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  33. * space (space) object 'IA' 'IL' 'IN')

Both reindex_like and align work interchangeably betweenDataArray and Dataset objects, and with any number of matching dimension names:

  1. In [94]: ds
  2. Out[94]:
  3. <xarray.Dataset>
  4. Dimensions: (lat: 25, lon: 53, time: 2920)
  5. Coordinates:
  6. * lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  7. * lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
  8. * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
  9. Data variables:
  10. air (time, lat, lon) float32 241.2 242.5 243.5 ... 296.49 296.19 295.69
  11. empty (lat, lon) float32 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
  12. Attributes:
  13. Conventions: COARDS
  14. title: 4x daily NMC reanalysis (1948)
  15. description: Data is from NMC initialized reanalysis\n(4x/day). These a...
  16. platform: Model
  17. references: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly...
  18.  
  19. In [95]: ds.reindex_like(baz)
  20. Out[95]:
  21. <xarray.Dataset>
  22. Dimensions: (lat: 25, lon: 53, time: 2)
  23. Coordinates:
  24. * time (time) datetime64[ns] 2000-01-01 2000-01-02
  25. * lat (lat) float64 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  26. * lon (lon) float64 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
  27. Data variables:
  28. air (time, lat, lon) float32 nan nan nan nan nan ... nan nan nan nan
  29. empty (lat, lon) float32 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
  30. Attributes:
  31. Conventions: COARDS
  32. title: 4x daily NMC reanalysis (1948)
  33. description: Data is from NMC initialized reanalysis\n(4x/day). These a...
  34. platform: Model
  35. references: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly...
  36.  
  37. In [96]: other = xr.DataArray(['a', 'b', 'c'], dims='other')
  38.  
  39. # this is a no-op, because there are no shared dimension names
  40. In [97]: ds.reindex_like(other)
  41. Out[97]:
  42. <xarray.Dataset>
  43. Dimensions: (lat: 25, lon: 53, time: 2920)
  44. Coordinates:
  45. * lat (lat) float64 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
  46. * lon (lon) float64 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
  47. * time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
  48. Data variables:
  49. air (time, lat, lon) float32 241.2 242.5 243.5 ... 296.49 296.19 295.69
  50. empty (lat, lon) float32 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
  51. Attributes:
  52. Conventions: COARDS
  53. title: 4x daily NMC reanalysis (1948)
  54. description: Data is from NMC initialized reanalysis\n(4x/day). These a...
  55. platform: Model
  56. references: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly...

Missing coordinate labels

Coordinate labels for each dimension are optional (as of xarray v0.9). Labelbased indexing with .sel and .loc uses standard positional,integer-based indexing as a fallback for dimensions without a coordinate label:

  1. In [98]: da = xr.DataArray([1, 2, 3], dims='x')
  2.  
  3. In [99]: da.sel(x=[0, -1])
  4. Out[99]:
  5. <xarray.DataArray (x: 2)>
  6. array([1, 3])
  7. Dimensions without coordinates: x

Alignment between xarray objects where one or both do not have coordinate labelssucceeds only if all dimensions of the same name have the same length.Otherwise, it raises an informative error:

  1. In [100]: xr.align(da, da[:2])
  2. ValueError: arguments without labels along dimension 'x' cannot be aligned because they have different dimension sizes: {2, 3}

Underlying Indexes

xarray uses the pandas.Index internally to perform indexingoperations. If you need to access the underlying indexes, they are availablethrough the indexes attribute.

  1. In [101]: da = xr.DataArray(np.random.rand(4, 3),
  2. .....: [('time', pd.date_range('2000-01-01', periods=4)),
  3. .....: ('space', ['IA', 'IL', 'IN'])])
  4. .....:
  5.  
  6. In [102]: da
  7. Out[102]:
  8. <xarray.DataArray (time: 4, space: 3)>
  9. array([[0.170917, 0.394659, 0.641666],
  10. [0.274592, 0.462354, 0.871372],
  11. [0.401131, 0.610588, 0.117967],
  12. [0.702184, 0.414034, 0.342345]])
  13. Coordinates:
  14. * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  15. * space (space) <U2 'IA' 'IL' 'IN'
  16.  
  17. In [103]: da.indexes
  18. Out[103]:
  19. time: DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04'], dtype='datetime64[ns]', name='time', freq='D')
  20. space: Index(['IA', 'IL', 'IN'], dtype='object', name='space')
  21.  
  22. In [104]: da.indexes['time']
  23. Out[104]: DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04'], dtype='datetime64[ns]', name='time', freq='D')

Use get_index() to get an index for a dimension,falling back to a default pandas.RangeIndex if it has no coordinatelabels:

  1. In [105]: da = xr.DataArray([1, 2, 3], dims='x')
  2.  
  3. In [106]: da
  4. Out[106]:
  5. <xarray.DataArray (x: 3)>
  6. array([1, 2, 3])
  7. Dimensions without coordinates: x
  8.  
  9. In [107]: da.get_index('x')
  10. Out[107]: RangeIndex(start=0, stop=3, step=1, name='x')

Copies vs. Views

Whether array indexing returns a view or a copy of the underlyingdata depends on the nature of the labels.

For positional (integer)indexing, xarray follows the same rules as NumPy:

  • Positional indexing with only integers and slices returns a view.

  • Positional indexing with arrays or lists returns a copy.

The rules for label based indexing are more complex:

  • Label-based indexing with only slices returns a view.

  • Label-based indexing with arrays returns a copy.

  • Label-based indexing with scalars returns a view or a copy, dependingupon if the corresponding positional indexer can be represented as aninteger or a slice object. The exact rules are determined by pandas.

Whether data is a copy or a view is more predictable in xarray than in pandas, sounlike pandas, xarray does not produce SettingWithCopy warnings. However, youshould still avoid assignment with chained indexing.

Multi-level indexing

Just like pandas, advanced indexing on multi-level indexes is possible withloc and sel. You can slice a multi-index by providing multiple indexers,i.e., a tuple of slices, labels, list of labels, or any selector allowed bypandas:

  1. In [108]: midx = pd.MultiIndex.from_product([list('abc'), [0, 1]],
  2. .....: names=('one', 'two'))
  3. .....:
  4.  
  5. In [109]: mda = xr.DataArray(np.random.rand(6, 3),
  6. .....: [('x', midx), ('y', range(3))])
  7. .....:
  8.  
  9. In [110]: mda
  10. Out[110]:
  11. <xarray.DataArray (x: 6, y: 3)>
  12. array([[0.595925, 0.199864, 0.099737],
  13. [0.734596, 0.016545, 0.481385],
  14. [0.095939, 0.497306, 0.838796],
  15. [0.897333, 0.732592, 0.758724],
  16. [0.560657, 0.471478, 0.138768],
  17. [0.094461, 0.942256, 0.134099]])
  18. Coordinates:
  19. * x (x) MultiIndex
  20. - one (x) object 'a' 'a' 'b' 'b' 'c' 'c'
  21. - two (x) int64 0 1 0 1 0 1
  22. * y (y) int64 0 1 2
  23.  
  24. In [111]: mda.sel(x=(list('ab'), [0]))
  25. Out[111]:
  26. <xarray.DataArray (x: 2, y: 3)>
  27. array([[0.595925, 0.199864, 0.099737],
  28. [0.095939, 0.497306, 0.838796]])
  29. Coordinates:
  30. * x (x) MultiIndex
  31. - one (x) object 'a' 'b'
  32. - two (x) int64 0 0
  33. * y (y) int64 0 1 2

You can also select multiple elements by providing a list of labels or tuples ora slice of tuples:

  1. In [112]: mda.sel(x=[('a', 0), ('b', 1)])
  2. Out[112]:
  3. <xarray.DataArray (x: 2, y: 3)>
  4. array([[0.595925, 0.199864, 0.099737],
  5. [0.897333, 0.732592, 0.758724]])
  6. Coordinates:
  7. * x (x) MultiIndex
  8. - one (x) object 'a' 'b'
  9. - two (x) int64 0 1
  10. * y (y) int64 0 1 2

Additionally, xarray supports dictionaries:

  1. In [113]: mda.sel(x={'one': 'a', 'two': 0})
  2. Out[113]:
  3. <xarray.DataArray (y: 3)>
  4. array([0.595925, 0.199864, 0.099737])
  5. Coordinates:
  6. x object ('a', 0)
  7. * y (y) int64 0 1 2

For convenience, sel also accepts multi-index levels directlyas keyword arguments:

  1. In [114]: mda.sel(one='a', two=0)
  2. Out[114]:
  3. <xarray.DataArray (y: 3)>
  4. array([0.595925, 0.199864, 0.099737])
  5. Coordinates:
  6. x object ('a', 0)
  7. * y (y) int64 0 1 2

Note that using sel it is not possible to mix a dimensionindexer with level indexers for that dimension(e.g., mda.sel(x={'one': 'a'}, two=0) will raise a ValueError).

Like pandas, xarray handles partial selection on multi-index (level drop).As shown below, it also renames the dimension / coordinate when themulti-index is reduced to a single index.

  1. In [115]: mda.loc[{'one': 'a'}, ...]
  2. Out[115]:
  3. <xarray.DataArray (two: 2, y: 3)>
  4. array([[0.595925, 0.199864, 0.099737],
  5. [0.734596, 0.016545, 0.481385]])
  6. Coordinates:
  7. * two (two) int64 0 1
  8. * y (y) int64 0 1 2

Unlike pandas, xarray does not guess whether you provide index levels ordimensions when using loc in some ambiguous cases. For example, formda.loc[{'one': 'a', 'two': 0}] and mda.loc['a', 0] xarrayalways interprets (‘one’, ‘two’) and (‘a’, 0) as the names andlabels of the 1st and 2nd dimension, respectively. You must specify alldimensions or use the ellipsis in the loc specifier, e.g. in the exampleabove, mda.loc[{'one': 'a', 'two': 0}, :] or mda.loc[('a', 0), …].

Indexing rules

Here we describe the full rules xarray uses for vectorized indexing. Note thatthis is for the purposes of explanation: for the sake of efficiency and tosupport various backends, the actual implementation is different.

  • (Only for label based indexing.) Look up positional indexes along eachdimension from the corresponding pandas.Index.

  • A full slice object : is inserted for each dimension without an indexer.

  • slice objects are converted into arrays, given bynp.arange(*slice.indices(…)).

  • Assume dimension names for array indexers without dimensions, such asnp.ndarray and list, from the dimensions to be indexed along.For example, v.isel(x=[0, 1]) is understood asv.isel(x=xr.DataArray([0, 1], dims=['x'])).

  • For each variable in a Dataset or DataArray (the array and itscoordinates):

    • Broadcast all relevant indexers based on their dimension names(see Broadcasting by dimension name for full details).

    • Index the underling array by the broadcast indexers, using NumPy’sadvanced indexing rules.

  • If any indexer DataArray has coordinates and no coordinate with thesame name exists, attach them to the indexed object.

Note

Only 1-dimensional boolean arrays can be used as indexers.