xarray.DataArray.reindex_like#

DataArray.reindex_like(other, method=None, tolerance=None, copy=True, fill_value=<NA>)[source]#

Conform this object onto the indexes of another object, filling in missing values with fill_value. The default fill value is NaN.

Parameters
  • other (Dataset or DataArray) – Object with an ‘indexes’ attribute giving a mapping from dimension names to pandas.Index objects, which provides coordinates upon which to index the variables in this dataset. The indexes on this other object need not be the same as the indexes on this dataset. Any mis-matched index values will be filled in with NaN, and any mis-matched dimension names will simply be ignored.

  • method ({None, "nearest", "pad", "ffill", "backfill", "bfill"}, optional) – Method to use for filling index values from other not found on this data array:

    • None (default): don’t fill gaps

    • pad / ffill: propagate last valid index value forward

    • backfill / bfill: propagate next valid index value backward

    • nearest: use nearest valid index value

  • tolerance (optional) – Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must satisfy the equation abs(index[indexer] - target) <= tolerance. Tolerance may be a scalar value, which applies the same tolerance to all values, or list-like, which applies variable tolerance per element. List-like must be the same size as the index and its dtype must exactly match the index’s type.

  • copy (bool, default: True) – If copy=True, data in the return value is always copied. If copy=False and reindexing is unnecessary, or can be performed with only slice operations, then the output may share memory with the input. In either case, a new xarray object is always returned.

  • fill_value (scalar or dict-like, optional) – Value to use for newly missing values. If a dict-like, maps variable names (including coordinates) to fill values. Use this data array’s name to refer to the data array’s values.

Returns

reindexed (DataArray) – Another dataset array, with this array’s data but coordinates from the other object.

Examples

>>> data = np.arange(12).reshape(4, 3)
>>> da1 = xr.DataArray(
...     data=data,
...     dims=["x", "y"],
...     coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
... )
>>> da1
<xarray.DataArray (x: 4, y: 3)>
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Coordinates:
  * x        (x) int64 10 20 30 40
  * y        (y) int64 70 80 90
>>> da2 = xr.DataArray(
...     data=data,
...     dims=["x", "y"],
...     coords={"x": [40, 30, 20, 10], "y": [90, 80, 70]},
... )
>>> da2
<xarray.DataArray (x: 4, y: 3)>
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Coordinates:
  * x        (x) int64 40 30 20 10
  * y        (y) int64 90 80 70

Reindexing with both DataArrays having the same coordinates set, but in different order:

>>> da1.reindex_like(da2)
<xarray.DataArray (x: 4, y: 3)>
array([[11, 10,  9],
       [ 8,  7,  6],
       [ 5,  4,  3],
       [ 2,  1,  0]])
Coordinates:
  * x        (x) int64 40 30 20 10
  * y        (y) int64 90 80 70

Reindexing with the other array having coordinates which the source array doesn’t have:

>>> data = np.arange(12).reshape(4, 3)
>>> da1 = xr.DataArray(
...     data=data,
...     dims=["x", "y"],
...     coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
... )
>>> da2 = xr.DataArray(
...     data=data,
...     dims=["x", "y"],
...     coords={"x": [20, 10, 29, 39], "y": [70, 80, 90]},
... )
>>> da1.reindex_like(da2)
<xarray.DataArray (x: 4, y: 3)>
array([[ 3.,  4.,  5.],
       [ 0.,  1.,  2.],
       [nan, nan, nan],
       [nan, nan, nan]])
Coordinates:
  * x        (x) int64 20 10 29 39
  * y        (y) int64 70 80 90

Filling missing values with the previous valid index with respect to the coordinates’ value:

>>> da1.reindex_like(da2, method="ffill")
<xarray.DataArray (x: 4, y: 3)>
array([[3, 4, 5],
       [0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
Coordinates:
  * x        (x) int64 20 10 29 39
  * y        (y) int64 70 80 90

Filling missing values while tolerating specified error for inexact matches:

>>> da1.reindex_like(da2, method="ffill", tolerance=5)
<xarray.DataArray (x: 4, y: 3)>
array([[ 3.,  4.,  5.],
       [ 0.,  1.,  2.],
       [nan, nan, nan],
       [nan, nan, nan]])
Coordinates:
  * x        (x) int64 20 10 29 39
  * y        (y) int64 70 80 90

Filling missing values with manually specified values:

>>> da1.reindex_like(da2, fill_value=19)
<xarray.DataArray (x: 4, y: 3)>
array([[ 3,  4,  5],
       [ 0,  1,  2],
       [19, 19, 19],
       [19, 19, 19]])
Coordinates:
  * x        (x) int64 20 10 29 39
  * y        (y) int64 70 80 90