Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pycompat ref in utils #502

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading