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

Generalize lazy backend indexing a little more #10078

Merged
merged 6 commits into from
Feb 28, 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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ New Features
By `Justus Magin <https://github.com/keewis>`_.
- Added experimental support for coordinate transforms (not ready for public use yet!) (:pull:`9543`)
By `Benoit Bovy <https://github.com/benbovy>`_.
- Support reading to `GPU memory with Zarr <https://zarr.readthedocs.io/en/stable/user-guide/gpu.html>`_ (:pull:`10078`).
By `Deepak Cherian <https://github.com/dcherian>`_.

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down
3 changes: 2 additions & 1 deletion xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ def safe_cast_to_index(array: Any) -> pd.Index:
"""
from xarray.core.dataarray import DataArray
from xarray.core.variable import Variable
from xarray.namedarray.pycompat import to_numpy

if isinstance(array, pd.Index):
index = array
Expand All @@ -468,7 +469,7 @@ def safe_cast_to_index(array: Any) -> pd.Index:
)
kwargs["dtype"] = "float64"

index = pd.Index(np.asarray(array), **kwargs)
index = pd.Index(to_numpy(array), **kwargs)

return _maybe_cast_to_cftimeindex(index)

Expand Down
4 changes: 2 additions & 2 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,8 @@ def explicit_indexing_adapter(
raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
result = raw_indexing_method(raw_key.tuple)
if numpy_indices.tuple:
# index the loaded np.ndarray
indexable = NumpyIndexingAdapter(result)
# index the loaded duck array
indexable = as_indexable(result)
result = apply_indexer(indexable, numpy_indices)
return result

Expand Down
13 changes: 13 additions & 0 deletions xarray/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
raise_if_dask_computes,
requires_dask,
)
from xarray.tests.arrays import DuckArrayWrapper

B = IndexerMaker(indexing.BasicIndexer)

Expand Down Expand Up @@ -1052,3 +1053,15 @@ def test_advanced_indexing_dask_array() -> None:
with raise_if_dask_computes():
actual = ds.b.sel(x=ds.a.data)
assert_identical(expected, actual)


def test_backend_indexing_non_numpy() -> None:
"""This model indexing of a Zarr store that reads to GPU memory."""
array = DuckArrayWrapper(np.array([1, 2, 3]))
indexed = indexing.explicit_indexing_adapter(
indexing.BasicIndexer((slice(1),)),
shape=array.shape,
indexing_support=indexing.IndexingSupport.BASIC,
raw_indexing_method=array.__getitem__,
)
np.testing.assert_array_equal(indexed.array, np.array([1]))
Loading