Skip to content

Commit

Permalink
Remove pycompat ref in utils (#502)
Browse files Browse the repository at this point in the history
* Remove pycompat ref in utils

* Add note to whatsnew
  • Loading branch information
aulemahal authored Feb 19, 2024
1 parent 2b33ce5 commit 10f297e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
24 changes: 21 additions & 3 deletions cf_xarray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,35 @@
cftime = None


def _is_duck_dask_array(x):
"""Return True if the input is a dask array."""
# Code copied and simplified from xarray < 2024.02 (xr.core.pycompat.is_duck_dask_array)
try:
from dask.base import is_dask_collection
except ImportError:
return False

return (
is_dask_collection(x)
and hasattr(x, "ndim")
and hasattr(x, "shape")
and hasattr(x, "dtype")
and (
(hasattr(x, "__array_function__") and hasattr(x, "__array_ufunc__"))
or hasattr(x, "__array_namespace__")
)
)


def _contains_cftime_datetimes(array) -> bool:
"""Check if an array contains cftime.datetime objects"""
# Copied / adapted from xarray.core.common
from xarray.core.pycompat import is_duck_dask_array

if cftime is None:
return False
else:
if array.dtype == np.dtype("O") and array.size > 0:
sample = array.ravel()[0]
if is_duck_dask_array(sample):
if _is_duck_dask_array(sample):
sample = sample.compute()
if isinstance(sample, np.ndarray):
sample = sample.item()
Expand Down
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
What's New
----------

v0.8.10 (unreleased)
====================
- Fix methods in ``utils`` to work with xarray >= 2024.02.0. By `Pascal Bourgault`_.

v0.8.9 (Feb 06, 2023)
=====================
- Convert integer (e.g. ``1``) units to string (e.g. ``"1"``) for pint. By `Justus Magin`_.
Expand Down

0 comments on commit 10f297e

Please sign in to comment.