Skip to content
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
3 changes: 1 addition & 2 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ def astype(data, dtype, *, xp=None, **kwargs):
if xp is None:
xp = get_array_namespace(data)

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

Expand Down
15 changes: 15 additions & 0 deletions xarray/tests/test_duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
raise_if_dask_computes,
requires_bottleneck,
requires_cftime,
requires_cupy,
requires_dask,
requires_pyarrow,
)
Expand Down Expand Up @@ -184,6 +185,20 @@ def test_where_extension_duck_array(self, categorical1, categorical2):
where_res == pd.Categorical(["cat1", "cat1", "cat2", "cat3", "cat1"])
).all()

@requires_cupy
def test_where_cupy_duck_array(self):
import cupy as cp

arr = cp.array([[cp.nan, cp.nan], [2, 3], [4, 5]])
mask = ~cp.isnan(arr)
da = DataArray(arr, dims=("x", "y"), name="example")
output = da.where(mask, 0)

expected = np.array([[0, 0], [2, 3], [4, 5]])

assert isinstance(output.data, cp.ndarray)
assert_array_equal(output.to_numpy(), expected)

def test_concatenate_extension_duck_array(self, categorical1, categorical2):
concate_res = concatenate(
[PandasExtensionArray(categorical1), PandasExtensionArray(categorical2)]
Expand Down
Loading