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

Linear interp with NaNs in nd indexer #4233

Merged
merged 10 commits into from
Aug 27, 2020
4 changes: 2 additions & 2 deletions xarray/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ def _localize(var, indexes_coords):
indexes = {}
for dim, [x, new_x] in indexes_coords.items():
index = x.to_index()
imin = index.get_loc(np.min(new_x.values), method="nearest")
imax = index.get_loc(np.max(new_x.values), method="nearest")
imin = index.get_loc(np.nanmin(new_x.values), method="nearest")
imax = index.get_loc(np.nanmax(new_x.values), method="nearest")
Copy link
Member

Choose a reason for hiding this comment

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

It looks that np.nanmin (and nanmax) supports np.datetime-dtype only with numpy>=1.18.
We can copy np.nanmin to our core/npcompat.py and call this function here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think they added some low level loops to fix this (numpy/numpy#14841) so guess we cannot copy nanmin over and have to use if LooseVersion(np. __version__) < LooseVersion("1.18") (or similar).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added some LooseVersion checks for numpy>=1.18 in the d044142 commit to the missing._localize function and to the test. Would this do?


indexes[dim] = slice(max(imin - 2, 0), imax + 2)
indexes_coords[dim] = (x[indexes[dim]], new_x)
Expand Down
19 changes: 19 additions & 0 deletions xarray/tests/test_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,25 @@ def test_interpolate_nd_nd():
da.interp(a=ia)


def test_interpolate_nd_with_nan():
mathause marked this conversation as resolved.
Show resolved Hide resolved
"""Interpolate an array with an nd indexer."""

# Create indexer into `a` with dimensions (y, x)
x = [0, 1, 2]
y = [10, 20]
c = {"x": x, "y": y}
a = np.arange(6, dtype=float).reshape(2, 3)
a[0, 1] = np.nan
ia = xr.DataArray(a, dims=("y", "x"), coords=c)

da = xr.DataArray([1, 2, 2], dims=("a"), coords={"a": [0, 2, 4]})
out = da.interp(a=ia)
expected = xr.DataArray(
[[1.0, np.nan, 2.0], [2.0, 2.0, np.nan]], dims=("y", "x"), coords=c
)
xr.testing.assert_allclose(out.drop_vars("a"), expected)


@pytest.mark.parametrize("method", ["linear"])
@pytest.mark.parametrize("case", [0, 1])
def test_interpolate_scalar(method, case):
Expand Down