Open keewis opened 3 weeks ago
Aren't there non-numeric scalars? Perhaps .ndim == 0
is a better check.
I think we should probably add a special case for NumPy scalars, to cast them to arrays. It's simpler for Xarray users to always have NumPy arrays.
Wouldn't
not isinstance(data, (np.generic, np.ndarray))
work then? With that scalars would be passed through to the np.asarray
call, just like np.ndarray
.
Wouldn't
not isinstance(data, (np.generic, np.ndarray))
work then? With that scalars would be passed through to the
np.asarray
call, just likenp.ndarray
.
Yes, that would do the trick.
What is your issue?
I'm not sure if this is a bad thing or not, but while writing tests @TomNicholas and I noticed that starting with
numpy=2.1
,Variable
objects may contain numpy scalars, especially as the result of an aggregation likemean
.This is caused by numpy scalars gaining a
__array_namespace__
method, which is then interpreted byas_compatible_data
as an actual array.To fix this, we could change https://github.com/pydata/xarray/blob/a04d857a03d1fb04317d636a7f23239cb9034491/xarray/core/variable.py#L313-L315 to
but not sure if that's worth it.
To reproduce, try this in an environment with
numpy>=2.1
: