xarray.Dataset.interp#
- Dataset.interp(coords=None, method='linear', assume_sorted=False, kwargs=None, method_non_numeric='nearest', **coords_kwargs)[source]#
Interpolate a Dataset onto new coordinates.
Performs univariate or multivariate interpolation of a Dataset onto new coordinates, utilizing either NumPy or SciPy interpolation routines.
Out-of-range values are filled with NaN, unless specified otherwise via kwargs to the numpy/scipy interpolant.
- Parameters:
coords (
dict
, optional) – Mapping from dimension names to the new coordinates. New coordinate can be a scalar, array-like or DataArray. If DataArrays are passed as new coordinates, their dimensions are used for the broadcasting. 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 interpolator. Valid options and their behavior depend which interpolant is used.method_non_numeric (
{"nearest", "pad", "ffill", "backfill", "bfill"}
, optional) – Method for non-numeric types. Passed on toDataset.reindex()
."nearest"
is used by default.**coords_kwargs (
{dim: coordinate, ...}
, optional) – The keyword arguments form ofcoords
. One of coords or coords_kwargs must be provided.
- Returns:
interpolated (
Dataset
) – New dataset on the new coordinates.
Notes
SciPy is required for certain interpolation methods.
- 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:
- Interpolation along one dimension of 1D data (`method=’linear’`)
Uses
numpy.interp()
, unless fill_value=’extrapolate’ is provided via kwargs.
- 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 ofnumpy.interp()
(as in the case of method=’linear’ for 1D data).
If method=’polynomial’, the order keyword argument must also be provided.
- Special interpolants for interpolation along one dimension of N-dimensional data (N ≥ 1)
- Depending on the method, the following interpolants from
scipy.interpolate
are used: “pchip”:
scipy.interpolate.PchipInterpolator
“barycentric”:
scipy.interpolate.BarycentricInterpolator
“krogh”:
scipy.interpolate.KroghInterpolator
- “akima” or “makima”:
scipy.interpolate.Akima1dInterpolator
(makima is handled by passing the makima flag).
- “akima” or “makima”:
- Depending on the method, the following interpolants from
- Interpolation along multiple dimensions of multi-dimensional data
- Uses
scipy.interpolate.interpn()
for methods {“linear”, “nearest”, “slinear”, “cubic”, “quintic”, “pchip”}.
- Uses
See also
- Manipulating Dimensions (Data Resolution)
Tutorial material on manipulating data resolution using
interp()
Examples
>>> ds = xr.Dataset( ... data_vars={ ... "a": ("x", [5, 7, 4]), ... "b": ( ... ("x", "y"), ... [[1, 4, 2, 9], [2, 7, 6, np.nan], [6, np.nan, 5, 8]], ... ), ... }, ... coords={"x": [0, 1, 2], "y": [10, 12, 14, 16]}, ... ) >>> ds <xarray.Dataset> Size: 176B Dimensions: (x: 3, y: 4) Coordinates: * x (x) int64 24B 0 1 2 * y (y) int64 32B 10 12 14 16 Data variables: a (x) int64 24B 5 7 4 b (x, y) float64 96B 1.0 4.0 2.0 9.0 2.0 7.0 6.0 nan 6.0 nan 5.0 8.0
1D interpolation with the default method (linear):
>>> ds.interp(x=[0, 0.75, 1.25, 1.75]) <xarray.Dataset> Size: 224B Dimensions: (x: 4, y: 4) Coordinates: * y (y) int64 32B 10 12 14 16 * x (x) float64 32B 0.0 0.75 1.25 1.75 Data variables: a (x) float64 32B 5.0 6.5 6.25 4.75 b (x, y) float64 128B 1.0 4.0 2.0 nan 1.75 ... nan 5.0 nan 5.25 nan
1D interpolation with a different method:
>>> ds.interp(x=[0, 0.75, 1.25, 1.75], method="nearest") <xarray.Dataset> Size: 224B Dimensions: (x: 4, y: 4) Coordinates: * y (y) int64 32B 10 12 14 16 * x (x) float64 32B 0.0 0.75 1.25 1.75 Data variables: a (x) float64 32B 5.0 7.0 7.0 4.0 b (x, y) float64 128B 1.0 4.0 2.0 9.0 2.0 7.0 ... nan 6.0 nan 5.0 8.0
1D extrapolation:
>>> ds.interp( ... x=[1, 1.5, 2.5, 3.5], ... method="linear", ... kwargs={"fill_value": "extrapolate"}, ... ) <xarray.Dataset> Size: 224B Dimensions: (x: 4, y: 4) Coordinates: * y (y) int64 32B 10 12 14 16 * x (x) float64 32B 1.0 1.5 2.5 3.5 Data variables: a (x) float64 32B 7.0 5.5 2.5 -0.5 b (x, y) float64 128B 2.0 7.0 6.0 nan 4.0 ... nan 12.0 nan 3.5 nan
2D interpolation:
>>> ds.interp(x=[0, 0.75, 1.25, 1.75], y=[11, 13, 15], method="linear") <xarray.Dataset> Size: 184B Dimensions: (x: 4, y: 3) Coordinates: * x (x) float64 32B 0.0 0.75 1.25 1.75 * y (y) int64 24B 11 13 15 Data variables: a (x) float64 32B 5.0 6.5 6.25 4.75 b (x, y) float64 96B 2.5 3.0 nan 4.0 5.625 ... nan nan nan nan nan