Skip to content

explicitly cast the dtype of where's condition parameter to bool #10087

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

Merged
merged 15 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
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
24 changes: 17 additions & 7 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,17 @@
return xp.empty_like(a, **kwargs)


def astype(data, dtype, **kwargs):
if hasattr(data, "__array_namespace__"):
def astype(data, dtype, *, xp=None, **kwargs):
if not hasattr(data, "__array_namespace__") and xp is None:
return data.astype(dtype, **kwargs)

if xp is None:
xp = get_array_namespace(data)
if xp == np:
# numpy currently doesn't have a astype:
return data.astype(dtype, **kwargs)
return xp.astype(data, dtype, **kwargs)
return data.astype(dtype, **kwargs)

if xp == np:
# numpy currently doesn't have a astype:
return data.astype(dtype, **kwargs)

Check warning on line 236 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / windows-latest py3.10

invalid value encountered in cast

Check warning on line 236 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / windows-latest py3.10

invalid value encountered in cast
return xp.astype(data, dtype, **kwargs)


def asarray(data, xp=np, dtype=None):
Expand Down Expand Up @@ -373,6 +376,13 @@
def where(condition, x, y):
"""Three argument where() with better dtype promotion rules."""
xp = get_array_namespace(condition, x, y)

dtype = xp.bool_ if hasattr(xp, "bool_") else xp.bool
if not is_duck_array(condition):
condition = asarray(condition, dtype=dtype, xp=xp)
else:
condition = astype(condition, dtype=dtype, xp=xp)

return xp.where(condition, *as_shared_dtype([x, y], xp=xp))


Expand Down
2 changes: 1 addition & 1 deletion xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def factorize(self) -> EncodedGroups:
# Restore these after the raveling
broadcasted_masks = broadcast(*masks)
mask = functools.reduce(np.logical_or, broadcasted_masks) # type: ignore[arg-type]
_flatcodes = where(mask, -1, _flatcodes)
_flatcodes = where(mask.data, -1, _flatcodes)

full_index = pd.MultiIndex.from_product(
(grouper.full_index.values for grouper in groupers),
Expand Down
1 change: 0 additions & 1 deletion xarray/tests/test_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ def test_unstack(arrays: tuple[xr.DataArray, xr.DataArray]) -> None:
assert_equal(actual, expected)


@pytest.mark.skip
def test_where() -> None:
np_arr = xr.DataArray(np.array([1, 0]), dims="x")
xp_arr = xr.DataArray(xp.asarray([1, 0]), dims="x")
Expand Down
Loading