Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ Bug Fixes
- Fix :py:func:`decode_cf` failing on integer-encoded time arrays that contain
NaT when running against numpy 2.5+.
By `Ian Hunt-Isaak <https://github.com/ianhi>`_.

- Fix ``TypeError: Implicit conversion to a NumPy array is not allowed`` when trying to
use :py:func:`open_mfdataset` with a backend engine reading to CuPy arrays.
By `Wei Ji Leong <https://github.com/weiji14>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
11 changes: 8 additions & 3 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
to_0d_array,
)
from xarray.namedarray.parallelcompat import get_chunked_array_type
from xarray.namedarray.pycompat import array_type, integer_types, is_chunked_array
from xarray.namedarray.pycompat import (
array_type,
integer_types,
is_chunked_array,
to_numpy,
)

if TYPE_CHECKING:
from xarray.core.extension_array import PandasExtensionArray
Expand Down Expand Up @@ -683,9 +688,9 @@ def __array__(
self, dtype: DTypeLike | None = None, /, *, copy: bool | None = None
) -> np.ndarray:
if Version(np.__version__) >= Version("2.0.0"):
return np.asarray(self.get_duck_array(), dtype=dtype, copy=copy)
return to_numpy(self.get_duck_array(), dtype=dtype, copy=copy)
Comment thread
dcherian marked this conversation as resolved.
Outdated
else:
return np.asarray(self.get_duck_array(), dtype=dtype)
return to_numpy(self.get_duck_array(), dtype=dtype)

def get_duck_array(self):
return self.array.get_duck_array()
Expand Down
2 changes: 1 addition & 1 deletion xarray/namedarray/pycompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def is_0d_dask_array(x: duckarray[Any, Any]) -> bool:


def to_numpy(
data: duckarray[Any, Any], **kwargs: dict[str, Any]
data: duckarray[Any, Any], **kwargs: Any
) -> np.ndarray[Any, np.dtype[Any]]:
from xarray.core.indexing import ExplicitlyIndexed
from xarray.namedarray.parallelcompat import get_chunked_array_type
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,14 @@ def test_implicit_indexing_adapter_copy_on_write() -> None:
assert isinstance(implicit[:], indexing.ImplicitToExplicitIndexingAdapter)


def test_implicit_indexing_adapter_duck_array() -> None:
array = DuckArrayWrapper(array=np.arange(10))
implicit = indexing.ImplicitToExplicitIndexingAdapter(
indexing.ArrayApiIndexingAdapter(array), indexing.BasicIndexer
)
np.testing.assert_array_equal(np.asarray(implicit), np.arange(10))


def test_outer_indexer_consistency_with_broadcast_indexes_vectorized() -> None:
def nonzero(x):
if isinstance(x, np.ndarray) and x.dtype.kind == "b":
Expand Down
Loading