Skip to content

Commit 0184702

Browse files
authored
Generalize lazy backend indexing a little more (#10078)
1 parent 5ea1e81 commit 0184702

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ New Features
3030
By `Justus Magin <https://github.com/keewis>`_.
3131
- Added experimental support for coordinate transforms (not ready for public use yet!) (:pull:`9543`)
3232
By `Benoit Bovy <https://github.com/benbovy>`_.
33+
- Support reading to `GPU memory with Zarr <https://zarr.readthedocs.io/en/stable/user-guide/gpu.html>`_ (:pull:`10078`).
34+
By `Deepak Cherian <https://github.com/dcherian>`_.
3335

3436
Breaking changes
3537
~~~~~~~~~~~~~~~~

xarray/core/indexes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ def safe_cast_to_index(array: Any) -> pd.Index:
442442
"""
443443
from xarray.core.dataarray import DataArray
444444
from xarray.core.variable import Variable
445+
from xarray.namedarray.pycompat import to_numpy
445446

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

471-
index = pd.Index(np.asarray(array), **kwargs)
472+
index = pd.Index(to_numpy(array), **kwargs)
472473

473474
return _maybe_cast_to_cftimeindex(index)
474475

xarray/core/indexing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,8 @@ def explicit_indexing_adapter(
10131013
raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
10141014
result = raw_indexing_method(raw_key.tuple)
10151015
if numpy_indices.tuple:
1016-
# index the loaded np.ndarray
1017-
indexable = NumpyIndexingAdapter(result)
1016+
# index the loaded duck array
1017+
indexable = as_indexable(result)
10181018
result = apply_indexer(indexable, numpy_indices)
10191019
return result
10201020

xarray/tests/test_indexing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
raise_if_dask_computes,
2020
requires_dask,
2121
)
22+
from xarray.tests.arrays import DuckArrayWrapper
2223

2324
B = IndexerMaker(indexing.BasicIndexer)
2425

@@ -1052,3 +1053,15 @@ def test_advanced_indexing_dask_array() -> None:
10521053
with raise_if_dask_computes():
10531054
actual = ds.b.sel(x=ds.a.data)
10541055
assert_identical(expected, actual)
1056+
1057+
1058+
def test_backend_indexing_non_numpy() -> None:
1059+
"""This model indexing of a Zarr store that reads to GPU memory."""
1060+
array = DuckArrayWrapper(np.array([1, 2, 3]))
1061+
indexed = indexing.explicit_indexing_adapter(
1062+
indexing.BasicIndexer((slice(1),)),
1063+
shape=array.shape,
1064+
indexing_support=indexing.IndexingSupport.BASIC,
1065+
raw_indexing_method=array.__getitem__,
1066+
)
1067+
np.testing.assert_array_equal(indexed.array, np.array([1]))

0 commit comments

Comments
 (0)