Skip to content

Commit

Permalink
add order for polynomial interpolation, fixes #8762 (#9079)
Browse files Browse the repository at this point in the history
* 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 <dcherian@users.noreply.github.com>

* 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 <dcherian@users.noreply.github.com>
  • Loading branch information
3 people authored and andersy005 committed Jun 14, 2024
1 parent d7604f1 commit 0fca670
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/nkarasiak>`_.


Documentation
~~~~~~~~~~~~~
Expand Down
9 changes: 5 additions & 4 deletions xarray/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)


Expand Down
18 changes: 14 additions & 4 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0fca670

Please sign in to comment.