Skip to content

RFC: Add more scipy interpolations methods #5067

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion xarray/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ def _get_interpolator(method, vectorizeable_only=False, **kwargs):
"quadratic",
"cubic",
"polynomial",
"previous",
"next",
]
valid_methods = interp1d_methods + [
"barycentric",
Expand Down Expand Up @@ -596,7 +598,7 @@ def interp(var, indexes_coords, method, **kwargs):
Note that all the coordinates should be Variable objects.
method : string
One of {'linear', 'nearest', 'zero', 'slinear', 'quadratic',
'cubic'}. For multidimensional interpolation, only
'cubic', 'previous', 'next'}. For multidimensional interpolation, only
{'linear', 'nearest'} can be used.
**kwargs
keyword arguments to be passed to scipy.interpolate
Expand Down
22 changes: 20 additions & 2 deletions xarray/tests/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ def make_interpolate_example_data(shape, frac_nan, seed=12345, non_uniform=False
def test_interpolate_pd_compat():
shapes = [(8, 8), (1, 20), (20, 1), (100, 100)]
frac_nans = [0, 0.5, 1]
methods = ["linear", "nearest", "zero", "slinear", "quadratic", "cubic"]
methods = [
"linear",
"nearest",
"zero",
"slinear",
"quadratic",
"cubic",
# "next",
# "previous",
]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we would add next and previous to the pandas compat tests as well. However, these interpolation methods are not wrapped by pandas yet, so they currently fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pandas has this functionality in pad/backfill for fillna() rather than in interpolate. That could be a good method to try for the tests here.


for (shape, frac_nan, method) in itertools.product(shapes, frac_nans, methods):

Expand Down Expand Up @@ -291,7 +300,16 @@ def test_interpolate_limits():

@requires_scipy
def test_interpolate_methods():
for method in ["linear", "nearest", "zero", "slinear", "quadratic", "cubic"]:
for method in [
"linear",
"nearest",
"zero",
"slinear",
"quadratic",
"cubic",
"previous",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should check that the output matches that from scipy since we can't use pandas for that

Can you add that check here?

"next",
]:
kwargs = {}
da = xr.DataArray(
np.array([0, 1, 2, np.nan, np.nan, np.nan, 6, 7, 8], dtype=np.float64),
Expand Down