Skip to content

Commit 3f7670b

Browse files
committed
Try updating result_type for array API
1 parent fcbd821 commit 3f7670b

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

xarray/core/dtypes.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def is_datetime_like(dtype):
168168

169169
def result_type(
170170
*arrays_and_dtypes: np.typing.ArrayLike | np.typing.DTypeLike,
171+
xp=np,
171172
) -> np.dtype:
172173
"""Like np.result_type, but with type promotion rules matching pandas.
173174
@@ -184,12 +185,13 @@ def result_type(
184185
-------
185186
numpy.dtype for the result.
186187
"""
187-
types = {np.result_type(t).type for t in arrays_and_dtypes}
188+
types = {xp.result_type(t) for t in arrays_and_dtypes}
188189

189-
for left, right in PROMOTE_TO_OBJECT:
190-
if any(issubclass(t, left) for t in types) and any(
191-
issubclass(t, right) for t in types
192-
):
193-
return np.dtype(object)
190+
if xp == np:
191+
for left, right in PROMOTE_TO_OBJECT:
192+
if any(issubclass(t.type, left) for t in types) and any(
193+
issubclass(t.type, right) for t in types
194+
):
195+
return np.dtype(object)
194196

195-
return np.result_type(*arrays_and_dtypes)
197+
return xp.result_type(*arrays_and_dtypes)

xarray/core/duck_array_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def as_shared_dtype(scalars_or_arrays, xp=np):
237237
# get handled properly.
238238
# Note that result_type() safely gets the dtype from dask arrays without
239239
# evaluating them.
240-
out_type = dtypes.result_type(*arrays)
240+
out_type = dtypes.result_type(*arrays, xp=xp)
241241
return [
242242
astype(x, out_type, copy=False) if hasattr(x, "dtype") else x for x in arrays
243243
]

0 commit comments

Comments
 (0)