From 0fca6708a41ee83b252ffe2969dc3af01cfcb0cd Mon Sep 17 00:00:00 2001 From: Nicolas Karasiak Date: Tue, 11 Jun 2024 15:51:16 +0200 Subject: [PATCH] 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 * 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 --- doc/whats-new.rst | 4 ++++ xarray/core/resample.py | 9 +++++---- xarray/tests/test_groupby.py | 18 ++++++++++++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 6e07caedc5a..96005e17f78 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -41,6 +41,10 @@ Deprecations Bug fixes ~~~~~~~~~ +- :py:meth:`DataArrayResample.interpolate` and :py:meth:`DatasetResample.interpolate` method now + support aribtrary kwargs such as ``order`` for polynomial interpolation. (:issue:`8762`). + By `Nicolas Karasiak `_. + Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/resample.py b/xarray/core/resample.py index 3bb158acfdb..9181bb4ee9b 100644 --- a/xarray/core/resample.py +++ b/xarray/core/resample.py @@ -140,7 +140,7 @@ def nearest(self, tolerance: float | Iterable[float] | None = None) -> T_Xarray: {self._dim: grouper.full_index}, method="nearest", tolerance=tolerance ) - def interpolate(self, kind: InterpOptions = "linear") -> T_Xarray: + def interpolate(self, kind: InterpOptions = "linear", **kwargs) -> T_Xarray: """Interpolate up-sampled data using the original data as knots. Parameters @@ -168,17 +168,18 @@ def interpolate(self, kind: InterpOptions = "linear") -> T_Xarray: scipy.interpolate.interp1d """ - return self._interpolate(kind=kind) + return self._interpolate(kind=kind, **kwargs) - def _interpolate(self, kind="linear") -> T_Xarray: + def _interpolate(self, kind="linear", **kwargs) -> T_Xarray: """Apply scipy.interpolate.interp1d along resampling dimension.""" obj = self._drop_coords() (grouper,) = self.groupers + kwargs.setdefault("bounds_error", False) return obj.interp( coords={self._dim: grouper.full_index}, assume_sorted=True, method=kind, - kwargs={"bounds_error": False}, + kwargs=kwargs, ) diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index a18b18f930c..47cda064143 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -2074,13 +2074,18 @@ def test_upsample_interpolate(self) -> None: "slinear", "quadratic", "cubic", + "polynomial", ] for kind in kinds: - actual = array.resample(time="1h").interpolate(kind) + kwargs = {} + if kind == "polynomial": + kwargs["order"] = 1 + actual = array.resample(time="1h").interpolate(kind, **kwargs) + # using interp1d, polynomial order is to set directly in kind using int f = interp1d( np.arange(len(times)), data, - kind=kind, + kind=kwargs["order"] if kind == "polynomial" else kind, axis=-1, bounds_error=True, assume_sorted=True, @@ -2147,14 +2152,19 @@ def test_upsample_interpolate_dask(self, chunked_time: bool) -> None: "slinear", "quadratic", "cubic", + "polynomial", ] for kind in kinds: - actual = array.chunk(chunks).resample(time="1h").interpolate(kind) + kwargs = {} + if kind == "polynomial": + kwargs["order"] = 1 + actual = array.chunk(chunks).resample(time="1h").interpolate(kind, **kwargs) actual = actual.compute() + # using interp1d, polynomial order is to set directly in kind using int f = interp1d( np.arange(len(times)), data, - kind=kind, + kind=kwargs["order"] if kind == "polynomial" else kind, axis=-1, bounds_error=True, assume_sorted=True,