Skip to content

Commit dc3e387

Browse files
committed
(fix): allow through scalars with extension arrays in result_type
1 parent d64eee6 commit dc3e387

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

xarray/core/dtypes.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,12 @@ def isdtype(dtype, kind: str | tuple[str, ...], xp=None) -> bool:
226226

227227
def maybe_promote_to_variable_width(
228228
array_or_dtype: np.typing.ArrayLike | np.typing.DTypeLike,
229+
*,
230+
should_return_str_or_bytes: bool = False,
229231
) -> np.typing.ArrayLike | np.typing.DTypeLike:
230232
if isinstance(array_or_dtype, str | bytes):
233+
if should_return_str_or_bytes:
234+
return array_or_dtype
231235
return type(array_or_dtype)
232236
elif isinstance(
233237
dtype := getattr(array_or_dtype, "dtype", array_or_dtype), np.dtype
@@ -300,5 +304,15 @@ def result_type(
300304
if should_promote_to_object(arrays_and_dtypes, xp):
301305
return np.dtype(object)
302306
return array_api_compat.result_type(
303-
*map(maybe_promote_to_variable_width, arrays_and_dtypes), xp=xp
307+
*map(
308+
functools.partial(
309+
maybe_promote_to_variable_width,
310+
# let extension arrays handle their own str/bytes
311+
should_return_str_or_bytes=any(
312+
map(is_extension_array_dtype, arrays_and_dtypes)
313+
),
314+
),
315+
arrays_and_dtypes,
316+
),
317+
xp=xp,
304318
)

0 commit comments

Comments
 (0)