Skip to content

Commit

Permalink
Added numpy>=1.18 checks with LooseVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssss committed Aug 22, 2020
2 parents 6c07668 + 5daa841 commit d044142
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Bug fixes
- Fixed overflow issue causing incorrect results in computing means of :py:class:`cftime.datetime`
arrays (:issue:`4341`). By `Spencer Clark <https://github.com/spencerkclark>`_.
- Fix `KeyError` when doing linear interpolation to an nd `DataArray`
that contains NaNs (:pull:`4233`).
that contains NaNs (:pull:`4233`). Requires `numpy` version `1.18` or newer.
By `Jens Svensmark <https://github.com/jenssss>`_


Expand Down
11 changes: 9 additions & 2 deletions xarray/core/missing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime as dt
import warnings
from distutils.version import LooseVersion
from functools import partial
from numbers import Number
from typing import Any, Callable, Dict, Hashable, Sequence, Union
Expand Down Expand Up @@ -551,8 +552,14 @@ def _localize(var, indexes_coords):
indexes = {}
for dim, [x, new_x] in indexes_coords.items():
index = x.to_index()
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"):
imin = index.get_loc(np.nanmin(new_x.values), method="nearest")
imax = index.get_loc(np.nanmax(new_x.values), method="nearest")
else:
imin = index.get_loc(np.min(new_x.values), method="nearest")
imax = index.get_loc(np.max(new_x.values), method="nearest")
# imin=0
# imax=0

indexes[dim] = slice(max(imin - 2, 0), imax + 2)
indexes_coords[dim] = (x[indexes[dim]], new_x)
Expand Down
7 changes: 6 additions & 1 deletion xarray/tests/test_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import xarray as xr
from xarray.tests import (
LooseVersion,
assert_allclose,
assert_equal,
assert_identical,
Expand Down Expand Up @@ -277,9 +278,13 @@ def test_interpolate_nd_nd():
da.interp(a=ia)


@pytest.mark.skipif(
LooseVersion(np.__version__) < "1.18",
reason="only in numpy>=1.18 does np.nanmin/max support np.datatime-dtype",
)
@requires_scipy
def test_interpolate_nd_with_nan():
"""Interpolate an array with an nd indexer."""
"""Interpolate an array with an nd indexer and `NaN` values."""

# Create indexer into `a` with dimensions (y, x)
x = [0, 1, 2]
Expand Down

0 comments on commit d044142

Please sign in to comment.