-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Labels
Description
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 like mean.
This is caused by numpy scalars gaining a __array_namespace__ method, which is then interpreted by as_compatible_data as an actual array.
To fix this, we could change
xarray/xarray/core/variable.py
Lines 313 to 315 in a04d857
| if not isinstance(data, np.ndarray) and ( | |
| hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__") | |
| ): |
if not isinstance(data, (np.numeric, np.ndarray)) and (
hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")
):but not sure if that's worth it.
To reproduce, try this in an environment with numpy>=2.1:
import numpy as np
import xarray as xr
v = xr.Variable((), np.float64(4.1))
repr(v)
# <xarray.Variable ()> Size: 8B
# np.float64(4.1)max-sixty and claydugo