Skip to content

Commit f0ee037

Browse files
nkarasiakpre-commit-ci[bot]dcherian
authored
add order for polynomial interpolation, fixes #8762 (#9079)
* add order for polynomial, fixes #8762 * add bugfixes in whats_new.rst, fixes #8762 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix typo * Update xarray/core/resample.py setdefault "bounds_error", and not forcing it. Thanks @dcherian Co-authored-by: Deepak Cherian <[email protected]> * chore(test) : polynomial resample * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix(setdefault) : change dict to arg * fix(test) : polynomial interpolate tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * chore(test) : add comment using interp1d for polynomial * Update xarray/tests/test_groupby.py * Update doc/whats-new.rst * Update whats-new.rst * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Deepak Cherian <[email protected]>
1 parent b636e68 commit f0ee037

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

doc/whats-new.rst

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Deprecations
4141
Bug fixes
4242
~~~~~~~~~
4343

44+
- :py:meth:`DataArrayResample.interpolate` and :py:meth:`DatasetResample.interpolate` method now
45+
support aribtrary kwargs such as ``order`` for polynomial interpolation. (:issue:`8762`).
46+
By `Nicolas Karasiak <https://github.com/nkarasiak>`_.
47+
4448

4549
Documentation
4650
~~~~~~~~~~~~~

xarray/core/resample.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def nearest(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray:
140140
{self._dim: grouper.full_index}, method="nearest", tolerance=tolerance
141141
)
142142

143-
def interpolate(self, kind: InterpOptions = "linear") -> T_Xarray:
143+
def interpolate(self, kind: InterpOptions = "linear", **kwargs) -> T_Xarray:
144144
"""Interpolate up-sampled data using the original data as knots.
145145
146146
Parameters
@@ -168,17 +168,18 @@ def interpolate(self, kind: InterpOptions = "linear") -> T_Xarray:
168168
scipy.interpolate.interp1d
169169
170170
"""
171-
return self._interpolate(kind=kind)
171+
return self._interpolate(kind=kind, **kwargs)
172172

173-
def _interpolate(self, kind="linear") -> T_Xarray:
173+
def _interpolate(self, kind="linear", **kwargs) -> T_Xarray:
174174
"""Apply scipy.interpolate.interp1d along resampling dimension."""
175175
obj = self._drop_coords()
176176
(grouper,) = self.groupers
177+
kwargs.setdefault("bounds_error", False)
177178
return obj.interp(
178179
coords={self._dim: grouper.full_index},
179180
assume_sorted=True,
180181
method=kind,
181-
kwargs={"bounds_error": False},
182+
kwargs=kwargs,
182183
)
183184

184185

xarray/tests/test_groupby.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -2074,13 +2074,18 @@ def test_upsample_interpolate(self) -> None:
20742074
"slinear",
20752075
"quadratic",
20762076
"cubic",
2077+
"polynomial",
20772078
]
20782079
for kind in kinds:
2079-
actual = array.resample(time="1h").interpolate(kind)
2080+
kwargs = {}
2081+
if kind == "polynomial":
2082+
kwargs["order"] = 1
2083+
actual = array.resample(time="1h").interpolate(kind, **kwargs)
2084+
# using interp1d, polynomial order is to set directly in kind using int
20802085
f = interp1d(
20812086
np.arange(len(times)),
20822087
data,
2083-
kind=kind,
2088+
kind=kwargs["order"] if kind == "polynomial" else kind,
20842089
axis=-1,
20852090
bounds_error=True,
20862091
assume_sorted=True,
@@ -2147,14 +2152,19 @@ def test_upsample_interpolate_dask(self, chunked_time: bool) -> None:
21472152
"slinear",
21482153
"quadratic",
21492154
"cubic",
2155+
"polynomial",
21502156
]
21512157
for kind in kinds:
2152-
actual = array.chunk(chunks).resample(time="1h").interpolate(kind)
2158+
kwargs = {}
2159+
if kind == "polynomial":
2160+
kwargs["order"] = 1
2161+
actual = array.chunk(chunks).resample(time="1h").interpolate(kind, **kwargs)
21532162
actual = actual.compute()
2163+
# using interp1d, polynomial order is to set directly in kind using int
21542164
f = interp1d(
21552165
np.arange(len(times)),
21562166
data,
2157-
kind=kind,
2167+
kind=kwargs["order"] if kind == "polynomial" else kind,
21582168
axis=-1,
21592169
bounds_error=True,
21602170
assume_sorted=True,

0 commit comments

Comments
 (0)