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 23, 2020
1 parent 2929491 commit 2152fb8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 4 additions & 4 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ New Features

Bug fixes
~~~~~~~~~
- Fix `KeyError` when doing linear interpolation to an nd `DataArray`
that contains NaNs (:pull:`4233`).
By `Jens Svensmark <https://github.com/jenssss>`_

- Variables which are chunked using dask only along some dimensions can be chunked while storing with zarr along previously
unchunked dimensions (:pull:`4312`) By `Tobias Kölling <https://github.com/d70-t>`_.
- Fixed a bug in backend caused by basic installation of Dask (:issue:`4164`, :pull:`4318`)
Expand All @@ -77,6 +73,10 @@ Bug fixes
and :py:meth:`DataArray.str.wrap` (:issue:`4334`). By `Mathias Hauser <https://github.com/mathause>`_.
- 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`). Requires `numpy` version `1.18` or newer.
By `Jens Svensmark <https://github.com/jenssss>`_


Documentation
~~~~~~~~~~~~~
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 2152fb8

Please sign in to comment.