diff --git a/cf_xarray/utils.py b/cf_xarray/utils.py index 5644774c..5d040262 100644 --- a/cf_xarray/utils.py +++ b/cf_xarray/utils.py @@ -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() diff --git a/doc/whats-new.rst b/doc/whats-new.rst index e15115d7..388c2e98 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -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`_.