interp_like

UnitsAwareDataArray.interp_like(other: T_Xarray, method: InterpOptions = 'linear', assume_sorted: bool = False, kwargs: Mapping[str, Any] | None = None) Self

Interpolate this object onto the coordinates of another object, filling out of range values with NaN.

If interpolating along a single existing dimension, scipy.interpolate.interp1d is called. When interpolating along multiple existing dimensions, an attempt is made to decompose the interpolation into multiple 1-dimensional interpolations. If this is possible, scipy.interpolate.interp1d is called. Otherwise, scipy.interpolate.interpn() is called.

Parameters:
  • other (Dataset or DataArray) – Object with an ‘indexes’ attribute giving a mapping from dimension names to an 1d array-like, which provides coordinates upon which to index the variables in this dataset. Missing values are skipped.

  • method ({"linear", "nearest", "zero", "slinear", "quadratic", "cubic", "polynomial"}, default: "linear") –

    The method used to interpolate. The method should be supported by the scipy interpolator:

    • {“linear”, “nearest”, “zero”, “slinear”, “quadratic”, “cubic”, “polynomial”} when interp1d is called.

    • {“linear”, “nearest”} when interpn is called.

    If "polynomial" is passed, the order keyword argument must also be provided.

  • assume_sorted (bool, default: False) – If False, values of coordinates that are interpolated over can be in any order and they are sorted first. If True, interpolated coordinates are assumed to be an array of monotonically increasing values.

  • kwargs (dict, optional) – Additional keyword passed to scipy’s interpolator.

Returns:

interpolated – Another dataarray by interpolating this dataarray’s data along the coordinates of 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": [10, 20, 29, 39], "y": [70, 80, 90]},
... )
>>> 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 10 20 29 39
  * y        (y) int64 24B 70 80 90

Interpolate the values in the coordinates of the other DataArray with respect to the source’s values:

>>> da2.interp_like(da1)
<xarray.DataArray (x: 4, y: 3)> Size: 96B
array([[0. , 1. , 2. ],
       [3. , 4. , 5. ],
       [6.3, 7.3, 8.3],
       [nan, nan, nan]])
Coordinates:
  * x        (x) int64 32B 10 20 30 40
  * y        (y) int64 24B 70 80 90

Could also extrapolate missing values:

>>> da2.interp_like(da1, kwargs={"fill_value": "extrapolate"})
<xarray.DataArray (x: 4, y: 3)> Size: 96B
array([[ 0. ,  1. ,  2. ],
       [ 3. ,  4. ,  5. ],
       [ 6.3,  7.3,  8.3],
       [ 9.3, 10.3, 11.3]])
Coordinates:
  * x        (x) int64 32B 10 20 30 40
  * y        (y) int64 24B 70 80 90

Notes

scipy is required. If the dataarray has object-type coordinates, reindex is used for these coordinates instead of the interpolation.

See also

DataArray.interp, DataArray.reindex_like