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

Allow wrapping astropy.units.Quantity #9705

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 10 additions & 3 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,17 @@ def convert_non_numpy_type(data):
else:
data = np.asarray(data)

_is_array_like = isinstance(data, np.ndarray | np.generic)
_is_nep18 = hasattr(data, "__array_function__")
_has_array_api = hasattr(data, "__array_namespace__")
_has_unit = hasattr(data, "_unit")

# Allow `astropy.units.Quantity`
if _is_array_like and (_is_nep18 or _has_array_api) and _has_unit:
return cast("T_DuckArray", data)

# immediately return array-like types except `numpy.ndarray` subclasses and `numpy` scalars
if not isinstance(data, np.ndarray | np.generic) and (
hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")
):
if not _is_array_like and (_is_nep18 or _has_array_api):
return cast("T_DuckArray", data)

# validate whether the data is valid data types. Also, explicitly cast `numpy`
Expand Down
Loading