Skip to content

Commit

Permalink
Fix upcasting with python builtin numbers and numpy 2
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Apr 15, 2024
1 parent 3ace2fb commit 88e778a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
zeros_like, # noqa
)
from numpy import concatenate as _concatenate
from numpy.core.multiarray import normalize_axis_index # type: ignore[attr-defined]
try:
# numpy 2.x
from numpy.lib.array_utils import normalize_axis_index # type: ignore[attr-defined]
except ImportError:
# numpy 1.x
from numpy.core.multiarray import normalize_axis_index # type: ignore[attr-defined]
from numpy.lib.stride_tricks import sliding_window_view # noqa

from xarray.core import dask_array_ops, dtypes, nputils
Expand Down Expand Up @@ -202,13 +207,15 @@ def as_shared_dtype(scalars_or_arrays, xp=np):

arrays = [asarray(x, xp=cp) for x in scalars_or_arrays]
else:
arrays = [asarray(x, xp=xp) for x in scalars_or_arrays]
#arrays = [asarray(x, xp=xp) for x in scalars_or_arrays]
arrays = [x if isinstance(x, (int, float, complex)) else asarray(x, xp=xp) for x in scalars_or_arrays]
# Pass arrays directly instead of dtypes to result_type so scalars
# get handled properly.
# Note that result_type() safely gets the dtype from dask arrays without
# evaluating them.
out_type = dtypes.result_type(*arrays)
return [astype(x, out_type, copy=False) for x in arrays]
#return [astype(x, out_type, copy=False) for x in arrays]
return [astype(x, out_type, copy=False) if hasattr(x, "dtype") else x for x in arrays]


def broadcast_to(array, shape):
Expand Down

0 comments on commit 88e778a

Please sign in to comment.