Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ filterwarnings = [
"default:the `pandas.MultiIndex` object:FutureWarning:xarray.tests.test_variable",
"default:Using a non-tuple sequence for multidimensional indexing is deprecated:FutureWarning",
"default:Duplicate dimension names present:UserWarning:xarray.namedarray.core",
# TODO: numpy.bool was deprecated in older versions of numpy, but is in the Array API
# TODO: remove once we can drop numpy<2
"ignore:In the future `np.bool` will be defined as the corresponding NumPy scalar:FutureWarning",
Comment thread
keewis marked this conversation as resolved.
Outdated
# TODO: this is raised for vlen-utf8, consolidated metadata, U1 dtype
"ignore:is currently not part .* the Zarr version 3 specification.",
# TODO: remove once we know how to deal with a changed signature in protocols
Expand Down
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 @@ def empty_like(a, **kwargs):
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)
return xp.astype(data, dtype, **kwargs)


def asarray(data, xp=np, dtype=None):
Expand Down Expand Up @@ -373,6 +376,13 @@ def sum_where(data, axis=None, dtype=None, where=None):
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)
Comment thread
keewis marked this conversation as resolved.

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