Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions xarray/core/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

from xarray.core import utils

PROMOTE_TO_OBJECT: tuple[tuple[type[np.generic], type[np.generic]], ...] = (
(np.number, np.character), # numpy promotes to character
(np.bool_, np.character), # numpy promotes to character
(np.bytes_, np.str_), # numpy promotes to unicode
)

# Use as a sentinel value to indicate a dtype appropriate NA value.
NA = utils.ReprObject("<NA>")

Expand Down Expand Up @@ -187,9 +193,17 @@ def result_type(
types = {np.result_type(t).type for t in arrays_and_dtypes}

for left, right in PROMOTE_TO_OBJECT:
if any(issubclass(t, left) for t in types) and any(
issubclass(t, right) for t in types
):
return np.dtype(object)
left_found = False
right_found = False
for t in types:
if not left_found and issubclass(t, left):
left_found = True
if right_found:
return np.dtype(object)
if not right_found and issubclass(t, right):
right_found = True
if left_found:
return np.dtype(object)
# If both found, already returned above

return np.result_type(*arrays_and_dtypes)