Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop the length from numpy's fixed-width string dtypes #9586

Merged
merged 8 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 8 additions & 3 deletions xarray/core/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,14 @@ def isdtype(dtype, kind: str | tuple[str, ...], xp=None) -> bool:
return xp.isdtype(dtype, kind)


def preprocess_scalar_types(t):
def preprocess_types(t):
if isinstance(t, str | bytes):
return type(t)
elif isinstance(
dtype := getattr(t, "dtype", t), np.dtypes.StrDType | np.dtypes.BytesDType
):
# drop the length from numpy's fixed-width string dtypes, it is better to recalculate that
return dtype.type
else:
return t

Expand Down Expand Up @@ -249,7 +254,7 @@ def result_type(
xp = get_array_namespace(arrays_and_dtypes)

types = {
array_api_compat.result_type(preprocess_scalar_types(t), xp=xp)
array_api_compat.result_type(preprocess_types(t), xp=xp)
for t in arrays_and_dtypes
}
if any(isinstance(t, np.dtype) for t in types):
Expand All @@ -262,5 +267,5 @@ def result_type(
return np.dtype(object)

return array_api_compat.result_type(
*map(preprocess_scalar_types, arrays_and_dtypes), xp=xp
*map(preprocess_types, arrays_and_dtypes), xp=xp
)
1 change: 1 addition & 0 deletions xarray/tests/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DummyArrayAPINamespace:
([np.str_, np.int64], np.object_),
([np.str_, np.str_], np.str_),
([np.bytes_, np.str_], np.object_),
([np.dtype("<U2"), np.str_], np.dtype("U")),
],
)
def test_result_type(args, expected) -> None:
Expand Down
Loading