Skip to content

Coordinates passed to interp have nan values #3924

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 1 commit into from
Closed
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: 2 additions & 2 deletions xarray/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,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")
Comment on lines +552 to +553
Copy link
Member

Choose a reason for hiding this comment

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

At first I was a little uneasy about your suggestion to require a newer version of NumPy, but after reading about the history of this issue a little more, I think something along those lines may actually be the simplest approach (perhaps you were ahead of me on this).

It turns out that np.min and np.max behave like np.nanmin and np.nanmax for time-like types in versions of NumPy prior to 1.18.1 (see numpy/numpy#14841 (comment)), so we can continue to use those for time-like types and earlier versions of NumPy and still obtain the desired behavior. I think something like the following would work:

Suggested change
imin = index.get_loc(np.nanmin(new_x.values), method="nearest")
imax = index.get_loc(np.nanmax(new_x.values), method="nearest")
if LooseVersion(np.__version__) < LooseVersion("1.18.1") and new_x.dtype.kind in "mM":
# In older versions of NumPy min and max (incorrectly) ignore NaT values
# for time-like types -- that is they behave like nanmin and nanmax
# should -- but nanmin and nanmax raise an error.
imin = index.get_loc(np.min(new_x.values), method="nearest")
imax = index.get_loc(np.max(new_x.values), method="nearest")
else:
imin = index.get_loc(np.nanmin(new_x.values), method="nearest")
imax = index.get_loc(np.nanmax(new_x.values), method="nearest")


indexes[dim] = slice(max(imin - 2, 0), imax + 2)
indexes_coords[dim] = (x[indexes[dim]], new_x)
Expand Down