Skip to content

Commit 10f297e

Browse files
authored
Remove pycompat ref in utils (#502)
* Remove pycompat ref in utils * Add note to whatsnew
1 parent 2b33ce5 commit 10f297e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

cf_xarray/utils.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,35 @@
1515
cftime = None
1616

1717

18+
def _is_duck_dask_array(x):
19+
"""Return True if the input is a dask array."""
20+
# Code copied and simplified from xarray < 2024.02 (xr.core.pycompat.is_duck_dask_array)
21+
try:
22+
from dask.base import is_dask_collection
23+
except ImportError:
24+
return False
25+
26+
return (
27+
is_dask_collection(x)
28+
and hasattr(x, "ndim")
29+
and hasattr(x, "shape")
30+
and hasattr(x, "dtype")
31+
and (
32+
(hasattr(x, "__array_function__") and hasattr(x, "__array_ufunc__"))
33+
or hasattr(x, "__array_namespace__")
34+
)
35+
)
36+
37+
1838
def _contains_cftime_datetimes(array) -> bool:
1939
"""Check if an array contains cftime.datetime objects"""
2040
# Copied / adapted from xarray.core.common
21-
from xarray.core.pycompat import is_duck_dask_array
22-
2341
if cftime is None:
2442
return False
2543
else:
2644
if array.dtype == np.dtype("O") and array.size > 0:
2745
sample = array.ravel()[0]
28-
if is_duck_dask_array(sample):
46+
if _is_duck_dask_array(sample):
2947
sample = sample.compute()
3048
if isinstance(sample, np.ndarray):
3149
sample = sample.item()

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
What's New
44
----------
55

6+
v0.8.10 (unreleased)
7+
====================
8+
- Fix methods in ``utils`` to work with xarray >= 2024.02.0. By `Pascal Bourgault`_.
9+
610
v0.8.9 (Feb 06, 2023)
711
=====================
812
- Convert integer (e.g. ``1``) units to string (e.g. ``"1"``) for pint. By `Justus Magin`_.

0 commit comments

Comments
 (0)