Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add order for polynomial interpolation, fixes #8762 #9079

Merged
merged 20 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f2856d6
add order for polynomial, fixes #8762
nkarasiak Jun 8, 2024
cb212e2
add bugfixes in whats_new.rst, fixes #8762
nkarasiak Jun 8, 2024
848c494
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 8, 2024
db04cfa
fix typo
nkarasiak Jun 8, 2024
e7208aa
Merge branch 'fix_polynomial_resample' of https://github.com/nkarasia…
nkarasiak Jun 8, 2024
9f71353
Update xarray/core/resample.py
nkarasiak Jun 10, 2024
65949d5
Merge branch 'fix_polynomial_resample' of https://github.com/nkarasia…
nkarasiak Jun 10, 2024
59e0234
chore(test) : polynomial resample
nkarasiak Jun 10, 2024
86110a5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 10, 2024
0865ca0
fix(setdefault) : change dict to arg
nkarasiak Jun 10, 2024
2e583fc
Merge branch 'fix_polynomial_resample' of https://github.com/nkarasia…
nkarasiak Jun 10, 2024
2a0eeda
fix(test) : polynomial interpolate tests
nkarasiak Jun 10, 2024
887a252
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 10, 2024
fb8aaea
chore(test) : add comment using interp1d for polynomial
nkarasiak Jun 10, 2024
e06aa80
Merge branch 'fix_polynomial_resample' of https://github.com/nkarasia…
nkarasiak Jun 10, 2024
b1270ee
Merge branch 'main' into fix_polynomial_resample
nkarasiak Jun 10, 2024
fddead0
Update xarray/tests/test_groupby.py
dcherian Jun 10, 2024
a2be756
Update doc/whats-new.rst
dcherian Jun 10, 2024
4dcfc86
Update whats-new.rst
dcherian Jun 11, 2024
fa84706
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Deprecations
Bug fixes
~~~~~~~~~

- Following `DataArrayResample.interpolate and passing kwargs
<https://github.com/pydata/xarray/issues/8762>`_ interpolate method now
supports kwargs such as order for producing the polynomial interpolation.
(:issue:`8762`). By
`Nicolas Karasiak <https://github.com/nkarasiak>`_.
dcherian marked this conversation as resolved.
Show resolved Hide resolved


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
Loading