Skip to content

Commit 3945342

Browse files
lstngrLouis Stengerpre-commit-ci[bot]
authored
Fix kwargs used for extrapolation in docs (#6639)
* Fix kwargs used for extrapolation in docs The current version of xarray tries to call scipy's interp1d whenever possible, and kwargs used in the user guide should reflect this. Fixes #6617 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add extended summary for interp methods The extended summaries for Dataset.interp and DataArray.interp explain how the scipy interpolator is selected * Update interp_like extended summary Explains how the scipy interpolator is chosen, similarly as done in Dataset.interp and DataArray.interp * Add the "polynomial" option to interp(_like) When scipy.interpolate.interp1d is called, it is possible to interpolate with the polynomial method if the `order` kwarg is provided. Co-authored-by: Louis Stenger <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e4f5b73 commit 3945342

File tree

3 files changed

+96
-33
lines changed

3 files changed

+96
-33
lines changed

doc/user-guide/interpolation.rst

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,12 @@ It is now possible to safely compute the difference ``other - interpolated``.
132132
Interpolation methods
133133
---------------------
134134

135-
We use :py:class:`scipy.interpolate.interp1d` for 1-dimensional interpolation and
136-
:py:func:`scipy.interpolate.interpn` for multi-dimensional interpolation.
135+
We use :py:class:`scipy.interpolate.interp1d` for 1-dimensional interpolation.
136+
For multi-dimensional interpolation, an attempt is first made to decompose the
137+
interpolation in a series of 1-dimensional interpolations, in which case
138+
:py:class:`scipy.interpolate.interp1d` is used. If a decomposition cannot be
139+
made (e.g. with advanced interpolation), :py:func:`scipy.interpolate.interpn` is
140+
used.
137141

138142
The interpolation method can be specified by the optional ``method`` argument.
139143

@@ -165,7 +169,9 @@ Additional keyword arguments can be passed to scipy's functions.
165169
[("time", np.arange(4)), ("space", [0.1, 0.2, 0.3])],
166170
)
167171
168-
da.interp(time=4, space=np.linspace(-0.1, 0.5, 10), kwargs={"fill_value": None})
172+
da.interp(
173+
time=4, space=np.linspace(-0.1, 0.5, 10), kwargs={"fill_value": "extrapolate"}
174+
)
169175
170176
171177
Advanced Interpolation
@@ -198,23 +204,28 @@ For example:
198204
y = xr.DataArray([0.1, 0.2, 0.3], dims="z")
199205
da.sel(x=x, y=y)
200206
201-
# advanced interpolation
202-
x = xr.DataArray([0.5, 1.5, 2.5], dims="z")
203-
y = xr.DataArray([0.15, 0.25, 0.35], dims="z")
207+
# advanced interpolation, without extrapolation
208+
x = xr.DataArray([0.5, 1.5, 2.5, 3.5], dims="z")
209+
y = xr.DataArray([0.15, 0.25, 0.35, 0.45], dims="z")
204210
da.interp(x=x, y=y)
205211
206212
where values on the original coordinates
207-
``(x, y) = ((0.5, 0.15), (1.5, 0.25), (2.5, 0.35))`` are obtained by the
208-
2-dimensional interpolation and mapped along a new dimension ``z``.
213+
``(x, y) = ((0.5, 0.15), (1.5, 0.25), (2.5, 0.35), (3.5, 0.45))`` are obtained
214+
by the 2-dimensional interpolation and mapped along a new dimension ``z``. Since
215+
no keyword arguments are passed to the interpolation routine, no extrapolation
216+
is performed resulting in a ``nan`` value.
209217

210218
If you want to add a coordinate to the new dimension ``z``, you can supply
211-
:py:class:`~xarray.DataArray` s with a coordinate,
219+
:py:class:`~xarray.DataArray` s with a coordinate. Extrapolation can be achieved
220+
by passing additional arguments to SciPy's ``interpnd`` function,
212221

213222
.. ipython:: python
214223
215-
x = xr.DataArray([0.5, 1.5, 2.5], dims="z", coords={"z": ["a", "b", "c"]})
216-
y = xr.DataArray([0.15, 0.25, 0.35], dims="z", coords={"z": ["a", "b", "c"]})
217-
da.interp(x=x, y=y)
224+
x = xr.DataArray([0.5, 1.5, 2.5, 3.5], dims="z", coords={"z": ["a", "b", "c", "d"]})
225+
y = xr.DataArray(
226+
[0.15, 0.25, 0.35, 0.45], dims="z", coords={"z": ["a", "b", "c", "d"]}
227+
)
228+
da.interp(x=x, y=y, kwargs={"fill_value": None})
218229
219230
For the details of the advanced indexing,
220231
see :ref:`more advanced indexing <more_advanced_indexing>`.

xarray/core/dataarray.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,29 +1758,42 @@ def interp(
17581758
kwargs: Mapping[str, Any] | None = None,
17591759
**coords_kwargs: Any,
17601760
) -> T_DataArray:
1761-
"""Multidimensional interpolation of variables.
1761+
"""Interpolate a DataArray onto new coordinates
1762+
1763+
Performs univariate or multivariate interpolation of a DataArray onto
1764+
new coordinates using scipy's interpolation routines. If interpolating
1765+
along an existing dimension, :py:class:`scipy.interpolate.interp1d` is
1766+
called. When interpolating along multiple existing dimensions, an
1767+
attempt is made to decompose the interpolation into multiple
1768+
1-dimensional interpolations. If this is possible,
1769+
:py:class:`scipy.interpolate.interp1d` is called. Otherwise,
1770+
:py:func:`scipy.interpolate.interpn` is called.
17621771
17631772
Parameters
17641773
----------
17651774
coords : dict, optional
17661775
Mapping from dimension names to the new coordinates.
1767-
New coordinate can be an scalar, array-like or DataArray.
1776+
New coordinate can be a scalar, array-like or DataArray.
17681777
If DataArrays are passed as new coordinates, their dimensions are
17691778
used for the broadcasting. Missing values are skipped.
1770-
method : {"linear", "nearest", "zero", "slinear", "quadratic", "cubic"}, default: "linear"
1771-
The method used to interpolate. Choose from
1779+
method : {"linear", "nearest", "zero", "slinear", "quadratic", "cubic", "polynomial"}, default: "linear"
1780+
The method used to interpolate. The method should be supported by
1781+
the scipy interpolator:
17721782
1773-
- {"linear", "nearest"} for multidimensional array,
1774-
- {"linear", "nearest", "zero", "slinear", "quadratic", "cubic"} for 1-dimensional array.
1783+
- ``interp1d``: {"linear", "nearest", "zero", "slinear",
1784+
"quadratic", "cubic", "polynomial"}
1785+
- ``interpn``: {"linear", "nearest"}
17751786
1787+
If ``"polynomial"`` is passed, the ``order`` keyword argument must
1788+
also be provided.
17761789
assume_sorted : bool, default: False
17771790
If False, values of x can be in any order and they are sorted
17781791
first. If True, x has to be an array of monotonically increasing
17791792
values.
17801793
kwargs : dict-like or None, default: None
17811794
Additional keyword arguments passed to scipy's interpolator. Valid
1782-
options and their behavior depend on if 1-dimensional or
1783-
multi-dimensional interpolation is used.
1795+
options and their behavior depend whether ``interp1d`` or
1796+
``interpn`` is used.
17841797
**coords_kwargs : {dim: coordinate, ...}, optional
17851798
The keyword arguments form of ``coords``.
17861799
One of coords or coords_kwargs must be provided.
@@ -1891,18 +1904,29 @@ def interp_like(
18911904
"""Interpolate this object onto the coordinates of another object,
18921905
filling out of range values with NaN.
18931906
1907+
If interpolating along a single existing dimension,
1908+
:py:class:`scipy.interpolate.interp1d` is called. When interpolating
1909+
along multiple existing dimensions, an attempt is made to decompose the
1910+
interpolation into multiple 1-dimensional interpolations. If this is
1911+
possible, :py:class:`scipy.interpolate.interp1d` is called. Otherwise,
1912+
:py:func:`scipy.interpolate.interpn` is called.
1913+
18941914
Parameters
18951915
----------
18961916
other : Dataset or DataArray
18971917
Object with an 'indexes' attribute giving a mapping from dimension
18981918
names to an 1d array-like, which provides coordinates upon
18991919
which to index the variables in this dataset. Missing values are skipped.
1900-
method : {"linear", "nearest", "zero", "slinear", "quadratic", "cubic"}, default: "linear"
1901-
The method used to interpolate. Choose from
1920+
method : {"linear", "nearest", "zero", "slinear", "quadratic", "cubic", "polynomial"}, default: "linear"
1921+
The method used to interpolate. The method should be supported by
1922+
the scipy interpolator:
19021923
1903-
- {"linear", "nearest"} for multidimensional array,
1904-
- {"linear", "nearest", "zero", "slinear", "quadratic", "cubic"} for 1-dimensional array.
1924+
- {"linear", "nearest", "zero", "slinear", "quadratic", "cubic",
1925+
"polynomial"} when ``interp1d`` is called.
1926+
- {"linear", "nearest"} when ``interpn`` is called.
19051927
1928+
If ``"polynomial"`` is passed, the ``order`` keyword argument must
1929+
also be provided.
19061930
assume_sorted : bool, default: False
19071931
If False, values of coordinates that are interpolated over can be
19081932
in any order and they are sorted first. If True, interpolated

xarray/core/dataset.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,7 +3044,16 @@ def interp(
30443044
method_non_numeric: str = "nearest",
30453045
**coords_kwargs: Any,
30463046
) -> Dataset:
3047-
"""Multidimensional interpolation of Dataset.
3047+
"""Interpolate a Dataset onto new coordinates
3048+
3049+
Performs univariate or multivariate interpolation of a Dataset onto
3050+
new coordinates using scipy's interpolation routines. If interpolating
3051+
along an existing dimension, :py:class:`scipy.interpolate.interp1d` is
3052+
called. When interpolating along multiple existing dimensions, an
3053+
attempt is made to decompose the interpolation into multiple
3054+
1-dimensional interpolations. If this is possible,
3055+
:py:class:`scipy.interpolate.interp1d` is called. Otherwise,
3056+
:py:func:`scipy.interpolate.interpn` is called.
30483057
30493058
Parameters
30503059
----------
@@ -3054,18 +3063,24 @@ def interp(
30543063
If DataArrays are passed as new coordinates, their dimensions are
30553064
used for the broadcasting. Missing values are skipped.
30563065
method : str, optional
3057-
{"linear", "nearest"} for multidimensional array,
3058-
{"linear", "nearest", "zero", "slinear", "quadratic", "cubic"}
3059-
for 1-dimensional array. "linear" is used by default.
3066+
The method used to interpolate. The method should be supported by
3067+
the scipy interpolator:
3068+
3069+
- ``interp1d``: {"linear", "nearest", "zero", "slinear",
3070+
"quadratic", "cubic", "polynomial"}
3071+
- ``interpn``: {"linear", "nearest"}
3072+
3073+
If ``"polynomial"`` is passed, the ``order`` keyword argument must
3074+
also be provided.
30603075
assume_sorted : bool, optional
30613076
If False, values of coordinates that are interpolated over can be
30623077
in any order and they are sorted first. If True, interpolated
30633078
coordinates are assumed to be an array of monotonically increasing
30643079
values.
30653080
kwargs : dict, optional
30663081
Additional keyword arguments passed to scipy's interpolator. Valid
3067-
options and their behavior depend on if 1-dimensional or
3068-
multi-dimensional interpolation is used.
3082+
options and their behavior depend whether ``interp1d`` or
3083+
``interpn`` is used.
30693084
method_non_numeric : {"nearest", "pad", "ffill", "backfill", "bfill"}, optional
30703085
Method for non-numeric types. Passed on to :py:meth:`Dataset.reindex`.
30713086
``"nearest"`` is used by default.
@@ -3307,16 +3322,29 @@ def interp_like(
33073322
"""Interpolate this object onto the coordinates of another object,
33083323
filling the out of range values with NaN.
33093324
3325+
If interpolating along a single existing dimension,
3326+
:py:class:`scipy.interpolate.interp1d` is called. When interpolating
3327+
along multiple existing dimensions, an attempt is made to decompose the
3328+
interpolation into multiple 1-dimensional interpolations. If this is
3329+
possible, :py:class:`scipy.interpolate.interp1d` is called. Otherwise,
3330+
:py:func:`scipy.interpolate.interpn` is called.
3331+
33103332
Parameters
33113333
----------
33123334
other : Dataset or DataArray
33133335
Object with an 'indexes' attribute giving a mapping from dimension
33143336
names to an 1d array-like, which provides coordinates upon
33153337
which to index the variables in this dataset. Missing values are skipped.
33163338
method : str, optional
3317-
{"linear", "nearest"} for multidimensional array,
3318-
{"linear", "nearest", "zero", "slinear", "quadratic", "cubic"}
3319-
for 1-dimensional array. 'linear' is used by default.
3339+
The method used to interpolate. The method should be supported by
3340+
the scipy interpolator:
3341+
3342+
- {"linear", "nearest", "zero", "slinear", "quadratic", "cubic",
3343+
"polynomial"} when ``interp1d`` is called.
3344+
- {"linear", "nearest"} when ``interpn`` is called.
3345+
3346+
If ``"polynomial"`` is passed, the ``order`` keyword argument must
3347+
also be provided.
33203348
assume_sorted : bool, optional
33213349
If False, values of coordinates that are interpolated over can be
33223350
in any order and they are sorted first. If True, interpolated

0 commit comments

Comments
 (0)