reindex_like

UnitsAwareDataArray.reindex_like(other: T_DataArrayOrSet, *, method: ReindexMethodOptions = None, tolerance: int | float | Iterable[int | float] | None = None, copy: bool = True, fill_value=<NA>) Self

Conform this object onto the indexes of another object, for indexes which the objects share. Missing values are filled 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 – Another dataset array, with this array’s data but coordinates from the other object.

Return type:

DataArray

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)> Size: 96B
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Coordinates:
  * x        (x) int64 32B 10 20 30 40
  * y        (y) int64 24B 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)> Size: 96B
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Coordinates:
  * x        (x) int64 32B 40 30 20 10
  * y        (y) int64 24B 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)> Size: 96B
array([[11, 10,  9],
       [ 8,  7,  6],
       [ 5,  4,  3],
       [ 2,  1,  0]])
Coordinates:
  * x        (x) int64 32B 40 30 20 10
  * y        (y) int64 24B 90 80 70

Reindexing with the other array having additional coordinates:

>>> da3 = xr.DataArray(
...     data=data,
...     dims=["x", "y"],
...     coords={"x": [20, 10, 29, 39], "y": [70, 80, 90]},
... )
>>> da1.reindex_like(da3)
<xarray.DataArray (x: 4, y: 3)> Size: 96B
array([[ 3.,  4.,  5.],
       [ 0.,  1.,  2.],
       [nan, nan, nan],
       [nan, nan, nan]])
Coordinates:
  * x        (x) int64 32B 20 10 29 39
  * y        (y) int64 24B 70 80 90

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

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

Filling missing values while tolerating specified error for inexact matches:

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

Filling missing values with manually specified values:

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

Note that unlike broadcast_like, reindex_like doesn’t create new dimensions:

>>> da1.sel(x=20)
<xarray.DataArray (y: 3)> Size: 24B
array([3, 4, 5])
Coordinates:
    x        int64 8B 20
  * y        (y) int64 24B 70 80 90

…so b in not added here:

>>> da1.sel(x=20).reindex_like(da1)
<xarray.DataArray (y: 3)> Size: 24B
array([3, 4, 5])
Coordinates:
    x        int64 8B 20
  * y        (y) int64 24B 70 80 90

See also

DataArray.reindex, DataArray.broadcast_like, align