reindex

UnitsAwareDataArray.reindex(indexers: Mapping[Any, Any] | None = None, *, method: ReindexMethodOptions = None, tolerance: float | Iterable[float] | None = None, copy: bool = True, fill_value=<NA>, **indexers_kwargs: Any) Self

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

Parameters:
  • indexers (dict, optional) – Dictionary with keys given by dimension names and values given by arrays of coordinates tick labels. Any mis-matched coordinate values will be filled in with NaN, and any mis-matched dimension names will simply be ignored. One of indexers or indexers_kwargs must be provided.

  • copy (bool, optional) – 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.

  • method ({None, 'nearest', 'pad'/'ffill', 'backfill'/'bfill'}, optional) –

    Method to use for filling index values in indexers 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 (float | Iterable[float] | None, default: None) – 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.

  • 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.

  • **indexers_kwargs ({dim: indexer, ...}, optional) – The keyword arguments form of indexers. One of indexers or indexers_kwargs must be provided.

Returns:

reindexed – Another dataset array, with this array’s data but replaced coordinates.

Return type:

DataArray

Examples

Reverse latitude:

>>> da = xr.DataArray(
...     np.arange(4),
...     coords=[np.array([90, 89, 88, 87])],
...     dims="lat",
... )
>>> da
<xarray.DataArray (lat: 4)> Size: 32B
array([0, 1, 2, 3])
Coordinates:
  * lat      (lat) int64 32B 90 89 88 87
>>> da.reindex(lat=da.lat[::-1])
<xarray.DataArray (lat: 4)> Size: 32B
array([3, 2, 1, 0])
Coordinates:
  * lat      (lat) int64 32B 87 88 89 90

See also

DataArray.reindex_like, align