xarray.DataArray.interp_like

xarray.DataArray.interp_like#

DataArray.interp_like(other, method='linear', assume_sorted=False, kwargs=None)[source]#

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

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", "quintic", "polynomial", "pchip", "barycentric", "krogh", "akima", "makima"}) – Interpolation method to use (see descriptions above).

  • 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 arguments passed to the interpolant.

Returns

interpolated (DataArray) – Another dataarray by interpolating this dataarray’s data along the coordinates of the other object.

Notes

  • scipy is required.

  • If the dataarray has object-type coordinates, reindex is used for these

    coordinates instead of the interpolation.

  • When interpolating along multiple dimensions with methods linear and nearest,

    the process attempts to decompose the interpolation into independent interpolations along one dimension at a time.

  • The specific interpolation method and dimensionality determine which

    interpolant is used:

    1. Interpolation along one dimension of 1D data (`method=’linear’`)
      • Uses numpy.interp(), unless fill_value=’extrapolate’ is provided via kwargs.

    2. Interpolation along one dimension of N-dimensional data (N ≥ 1)
      • Methods {“linear”, “nearest”, “zero”, “slinear”, “quadratic”, “cubic”, “quintic”, “polynomial”}

        use scipy.interpolate.interp1d(), unless conditions permit the use of numpy.interp() (as in the case of method=’linear’ for 1D data).

      • If method=’polynomial’, the order keyword argument must also be provided.

    3. Special interpolants for interpolation along one dimension of N-dimensional data (N ≥ 1)
    4. Interpolation along multiple dimensions of multi-dimensional data

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