Skip to content

Add asynchronous load method #10327

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

Open
wants to merge 144 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
144 commits
Select commit Hold shift + click to select a range
01e7518
new blank whatsnew
TomNicholas Oct 24, 2024
83e553b
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Oct 24, 2024
e44326d
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Nov 8, 2024
4e4eeb0
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Nov 20, 2024
d858059
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Nov 21, 2024
d377780
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Nov 21, 2024
3132f6a
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Nov 23, 2024
900eef5
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Nov 29, 2024
4c4462f
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Dec 4, 2024
5b9b749
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Jan 6, 2025
fadb953
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Jan 8, 2025
57d9d23
Merge branch 'main' of https://github.com/TomNicholas/xarray
TomNicholas Mar 13, 2025
11170fc
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Mar 19, 2025
0b8fa41
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Mar 20, 2025
f769f85
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Mar 20, 2025
4eef318
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Apr 7, 2025
29242a4
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas May 15, 2025
e6b3b3b
test async load using special zarr LatencyStore
TomNicholas May 15, 2025
3ceab60
don't use dask
TomNicholas May 16, 2025
071c35a
async all the way down
TomNicholas May 16, 2025
29374f9
remove assert False
TomNicholas May 16, 2025
ab12bb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 16, 2025
62aa39d
add pytest-asyncio to CI envs
TomNicholas May 16, 2025
dfe8bf7
Merge branch 'async.load' of https://github.com/TomNicholas/xarray in…
TomNicholas May 16, 2025
a906dec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 16, 2025
629ab31
assert results are identical
TomNicholas May 16, 2025
7e9ae0f
implement async load for dataarray and dataset
TomNicholas May 17, 2025
d288351
factor out common logic
TomNicholas May 17, 2025
e0731a0
consolidate tests via a parametrized fixture
TomNicholas May 17, 2025
9b41e78
async_load -> load_async
TomNicholas May 17, 2025
67ba26a
make BackendArray an ABC
TomNicholas May 18, 2025
9344e2e
explain how to add async support for any backend in the docs
TomNicholas May 18, 2025
f8f8563
add new methods to api docs
TomNicholas May 19, 2025
30ce9be
whatsnew
TomNicholas May 19, 2025
5d15bbd
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas May 19, 2025
1f02de1
Merge branch 'main' into async.load
TomNicholas May 19, 2025
2342b50
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 19, 2025
b6d4a82
Fix ci/minimum_versions.py
TomNicholas May 19, 2025
2079d7e
fix formatting
TomNicholas May 21, 2025
48e4534
concurrently load different variables in ds.load_async using asyncio.…
TomNicholas May 21, 2025
cca7589
test concurrent loading of multiple variables in one dataset
TomNicholas May 21, 2025
dfe9b87
fix non-awaited load_async
TomNicholas May 21, 2025
84099f3
rearrange test order
TomNicholas May 21, 2025
ab000c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 21, 2025
a8b7b46
add test for orthogonal indexing
TomNicholas May 23, 2025
82c7654
explicitly forbid orthogonal indexing
TomNicholas May 23, 2025
5eacdb0
support async orthogonal indexing via https://github.com/zarr-develop…
TomNicholas May 23, 2025
9f33c09
Merge branch 'async.load' of https://github.com/TomNicholas/xarray in…
TomNicholas May 23, 2025
093bf50
add test for vectorized indexing (even if it doesn't work)
TomNicholas May 23, 2025
4073a24
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 23, 2025
842a06c
add test for basic indexing
TomNicholas May 23, 2025
e19ab55
correct test to actually use vectorized indexing
TomNicholas May 23, 2025
b9e8e06
refactor to parametrize indexing test
TomNicholas May 23, 2025
8bc7bea
implement async vectorized indexing
TomNicholas May 29, 2025
6c47e3f
revert breaking change to BackendArray
TomNicholas May 29, 2025
a86f646
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 29, 2025
884ce13
remove indirection in _ensure_cached method
TomNicholas May 29, 2025
a43af86
IndexingAdapters don't need async get
dcherian May 29, 2025
17d7a0e
Add tests
dcherian May 29, 2025
d824a2d
Add decoding test
dcherian May 29, 2025
6a13611
Add IndexingAdapter mixin
dcherian May 29, 2025
d79ed54
[cherry] Making decoding arrays lazy too
dcherian May 30, 2025
1da3359
parametrized over isel and sel
TomNicholas May 30, 2025
dded9e0
mock zarr.AsyncArray.getitem in test
TomNicholas May 30, 2025
4c347ad
tidy up the mocking
TomNicholas May 30, 2025
b4ed8ee
Merge branch 'main' of https://github.com/pydata/xarray
TomNicholas Jun 20, 2025
0b55247
Merge branch 'main' into async.load
TomNicholas Jun 20, 2025
4018e28
ensure the correct zarr class's method is patched for each test
TomNicholas Jun 20, 2025
6da81ce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 20, 2025
7972164
add degenerate test case of no indexing
TomNicholas Jun 20, 2025
618424a
factor out the Latency part of LatencyStore
TomNicholas Jun 20, 2025
cd97481
use mocks in multiple objects test
TomNicholas Jun 20, 2025
75abdec
use mocks in multiple variables test
TomNicholas Jun 20, 2025
7409372
trim latencystore down to just what's needed to dodge https://github.…
TomNicholas Jun 20, 2025
1f79034
parametrizing indexing test over xarray classes
TomNicholas Jun 20, 2025
9881e8d
ensure we actually test vectorized indexing for Variable
TomNicholas Jun 20, 2025
1197798
use create_test_data
TomNicholas Jun 20, 2025
642fd48
add @pytest.mark.asyncio
dcherian Jun 20, 2025
a5a44f5
Merge branch 'main' into async.load
TomNicholas Jul 22, 2025
f22b56b
remove outdated readonly_store
TomNicholas Jul 22, 2025
ebfede5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 22, 2025
e882914
Merge branch 'main' into async.load
TomNicholas Jul 30, 2025
87c7fcb
enable tests to run when recent version of zarr-python is not available
TomNicholas Jul 31, 2025
67c77cc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 31, 2025
0d4bb0f
separate tests to only run on correct versions of zarr
TomNicholas Aug 1, 2025
ed2c808
clear error message if async oindexing not available
TomNicholas Aug 1, 2025
df32020
parametrize over zarr_format
TomNicholas Aug 1, 2025
aba2917
Merge branch 'async.load' of https://github.com/TomNicholas/xarray in…
TomNicholas Aug 1, 2025
d3e6a64
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 1, 2025
4570aed
add pytest-asyncio to other test CI env
TomNicholas Aug 1, 2025
ddde08e
Merge branch 'main' into async.load
TomNicholas Aug 1, 2025
0ec670e
fix some mypy errors
TomNicholas Aug 1, 2025
a3a3b62
use method directly when possible
TomNicholas Aug 4, 2025
b747f1c
Merge branch 'main' into async.load
TomNicholas Aug 4, 2025
a28a6a9
remove repeated API docs from bad merge
TomNicholas Aug 4, 2025
577cc72
fix bad merge in release note
TomNicholas Aug 4, 2025
a68579f
fix other bad merge in whatsnew
TomNicholas Aug 4, 2025
acc5c94
remove prints
TomNicholas Aug 4, 2025
7776d41
remove last print statement
TomNicholas Aug 4, 2025
0b1ebb5
test async basic indexing raises informative error before zarr-python…
TomNicholas Aug 4, 2025
df09780
test correct error message is raised for each indexing case
TomNicholas Aug 4, 2025
84f8e30
ensure each test runs on the earliest version of xaarr it can
TomNicholas Aug 4, 2025
19090b0
remove pointless repeated getitem
TomNicholas Aug 4, 2025
49416db
set N_LAZY_VARS correctly in test
TomNicholas Aug 4, 2025
2ed8455
remove unused import
TomNicholas Aug 4, 2025
a8a2860
rename flag to make it more clear its only for orthogonal and vectori…
TomNicholas Aug 4, 2025
ef6afdf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 4, 2025
de98308
remove IndexingAdapter special case
TomNicholas Aug 6, 2025
e32ea13
type fixes
dcherian Aug 6, 2025
da2d43c
return a deepcopy
TomNicholas Aug 6, 2025
ac3127f
Merge branch 'async.load' of https://github.com/TomNicholas/xarray in…
TomNicholas Aug 6, 2025
d46fc3f
try again
dcherian Aug 6, 2025
cc253c7
one more
dcherian Aug 6, 2025
78c9116
Try again
dcherian Aug 6, 2025
a727ecb
try fixing _in_memory error by not returning the adapter class
TomNicholas Aug 11, 2025
959edc2
Merge branch 'main' into async.load
TomNicholas Aug 11, 2025
9b7afc2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2025
b4ef26f
remove scope=module from fixture for robustness
TomNicholas Aug 11, 2025
a7918e4
modify test to be happy with either error message
TomNicholas Aug 11, 2025
199d50a
use Variable instead of Dataset to avoid race condition of indexing b…
TomNicholas Aug 11, 2025
1cbe913
fix bad merge in API docs
TomNicholas Aug 12, 2025
46d9414
add test to test_backends.py
TomNicholas Aug 12, 2025
b4a5a90
fix bug found by new test, causing pandas indexes to be converted to …
TomNicholas Aug 12, 2025
c5bed0d
Merge branch 'main' into async.load
TomNicholas Aug 12, 2025
bc1fe4e
add test to test_variable.py for lazy async indexing
TomNicholas Aug 12, 2025
10f7e61
move async load tests from test_async.py to test_backends.py
TomNicholas Aug 12, 2025
8416f00
parametrize all tests over zarr_format
TomNicholas Aug 12, 2025
4edd503
remove test_async.py file entirely
TomNicholas Aug 12, 2025
fcb2c11
lint
TomNicholas Aug 12, 2025
4760574
Stricter assertions
TomNicholas Aug 13, 2025
8bce3bb
Update doc/whats-new.rst
TomNicholas Aug 13, 2025
37be891
add docstring for Variable.load_async
TomNicholas Aug 13, 2025
3bd7b8a
make all load-related docstrings consistent
TomNicholas Aug 13, 2025
dfaac7e
note about users being responsible for limiting concurrency
TomNicholas Aug 13, 2025
d0a129a
remove parametrization over zarr_format
TomNicholas Aug 13, 2025
f30a3a0
account for Dataset having multiple lazy vars
TomNicholas Aug 13, 2025
432bbd5
refactor test parametrization to use pytest.param(..., id=...) syntax
TomNicholas Aug 13, 2025
4f40792
refactor TestBackendIndexing to combine sync and async checks in one …
TomNicholas Aug 13, 2025
cf1d127
move test_load_async onto test base class
TomNicholas Aug 13, 2025
54ab88c
Merge branch 'main' into async.load
TomNicholas Aug 13, 2025
dda58bf
should fix mypy error
TomNicholas Aug 13, 2025
02d661d
add back in the parametrize_zarr_format to avoid trying to write v3 d…
TomNicholas Aug 13, 2025
a074a25
parametrize test over async
TomNicholas Aug 13, 2025
f8f5e82
Merge branch 'main' into async.load
TomNicholas Aug 13, 2025
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
1 change: 1 addition & 0 deletions ci/minimum_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"coveralls",
"pip",
"pytest",
"pytest-asyncio",
"pytest-cov",
"pytest-env",
"pytest-mypy-plugins",
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/all-but-dask.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies:
- pip
- pydap
- pytest
- pytest-asyncio
- pytest-cov
- pytest-env
- pytest-mypy-plugins
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/all-but-numba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies:
- pyarrow # pandas raises a deprecation warning without this, breaking doctests
- pydap
- pytest
- pytest-asyncio
- pytest-cov
- pytest-env
- pytest-mypy-plugins
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/bare-min-and-scipy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies:
- coveralls
- pip
- pytest
- pytest-asyncio
- pytest-cov
- pytest-env
- pytest-mypy-plugins
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/bare-minimum.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies:
- coveralls
- pip
- pytest
- pytest-asyncio
- pytest-cov
- pytest-env
- pytest-mypy-plugins
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/environment-3.14.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies:
- pyarrow # pandas raises a deprecation warning without this, breaking doctests
- pydap
- pytest
- pytest-asyncio
- pytest-cov
- pytest-env
- pytest-mypy-plugins
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/environment-windows-3.14.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies:
- pyarrow # importing dask.dataframe raises an ImportError without this
- pydap
- pytest
- pytest-asyncio
- pytest-cov
- pytest-env
- pytest-mypy-plugins
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/environment-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies:
- pyarrow # importing dask.dataframe raises an ImportError without this
- pydap
- pytest
- pytest-asyncio
- pytest-cov
- pytest-env
- pytest-mypy-plugins
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies:
- pydap
- pydap-server
- pytest
- pytest-asyncio
- pytest-cov
- pytest-env
- pytest-mypy-plugins
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/min-all-deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:
- pip
- pydap=3.5.0
- pytest
- pytest-asyncio
- pytest-cov
- pytest-env
- pytest-mypy-plugins
Expand Down
1 change: 1 addition & 0 deletions doc/api-hidden.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
Variable.isnull
Variable.item
Variable.load
Variable.load_async
Variable.max
Variable.mean
Variable.median
Expand Down
48 changes: 33 additions & 15 deletions doc/internals/how-to-add-new-backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,12 @@ information on plugins.
How to support lazy loading
+++++++++++++++++++++++++++

If you want to make your backend effective with big datasets, then you should
support lazy loading.
Basically, you shall replace the :py:class:`numpy.ndarray` inside the
variables with a custom class that supports lazy loading indexing.
If you want to make your backend effective with big datasets, then you should take advantage of xarray's
support for lazy loading and indexing.

Basically, when your backend constructs the ``Variable`` objects,
you need to replace the :py:class:`numpy.ndarray` inside the
variables with a custom :py:class:`~xarray.backends.BackendArray` subclass that supports lazy loading and indexing.
See the example below:

.. code-block:: python
Expand All @@ -345,25 +347,27 @@ See the example below:

Where:

- :py:class:`~xarray.core.indexing.LazilyIndexedArray` is a class
provided by Xarray that manages the lazy loading.
- ``MyBackendArray`` shall be implemented by the backend and shall inherit
- :py:class:`~xarray.core.indexing.LazilyIndexedArray` is a wrapper class
provided by Xarray that manages the lazy loading and indexing.
- ``MyBackendArray`` should be implemented by the backend and must inherit
from :py:class:`~xarray.backends.BackendArray`.

BackendArray subclassing
^^^^^^^^^^^^^^^^^^^^^^^^

The BackendArray subclass shall implement the following method and attributes:
The BackendArray subclass must implement the following method and attributes:

- the ``__getitem__`` method that takes in input an index and returns a
`NumPy <https://numpy.org/>`__ array
- the ``shape`` attribute
- the ``__getitem__`` method that takes an index as an input and returns a
`NumPy <https://numpy.org/>`__ array,
- the ``shape`` attribute,
- the ``dtype`` attribute.

Xarray supports different type of :doc:`/user-guide/indexing`, that can be
grouped in three types of indexes
It may also optionally implement an additional ``async_getitem`` method.

Xarray supports different types of :doc:`/user-guide/indexing`, that can be
grouped in three types of indexes:
:py:class:`~xarray.core.indexing.BasicIndexer`,
:py:class:`~xarray.core.indexing.OuterIndexer` and
:py:class:`~xarray.core.indexing.OuterIndexer`, and
:py:class:`~xarray.core.indexing.VectorizedIndexer`.
This implies that the implementation of the method ``__getitem__`` can be tricky.
In order to simplify this task, Xarray provides a helper function,
Expand Down Expand Up @@ -419,8 +423,22 @@ input the ``key``, the array ``shape`` and the following parameters:
For more details see
:py:class:`~xarray.core.indexing.IndexingSupport` and :ref:`RST indexing`.

Async support
^^^^^^^^^^^^^

Backends can also optionally support loading data asynchronously via xarray's asynchronous loading methods
(e.g. ``~xarray.Dataset.load_async``).
To support async loading the ``BackendArray`` subclass must additionally implement the ``BackendArray.async_getitem`` method.

Note that implementing this method is only necessary if you want to be able to load data from different xarray objects concurrently.
Even without this method your ``BackendArray`` implementation is still free to concurrently load chunks of data for a single ``Variable`` itself,
so long as it does so behind the synchronous ``__getitem__`` interface.

Dask support
^^^^^^^^^^^^

In order to support `Dask Distributed <https://distributed.dask.org/>`__ and
:py:mod:`multiprocessing`, ``BackendArray`` subclass should be serializable
:py:mod:`multiprocessing`, the ``BackendArray`` subclass should be serializable
either with :ref:`io.pickle` or
`cloudpickle <https://github.com/cloudpipe/cloudpickle>`__.
That implies that all the reference to open files should be dropped. For
Expand Down
5 changes: 3 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ New Features
Useful for cleaning up DataTree after time-based filtering operations (:issue:`10590`, :pull:`10598`).
By `Alfonso Ladino <https://github.com/aladinor>`_.

- Added new asynchronous loading methods :py:meth:`Dataset.load_async`, :py:meth:`DataArray.load_async`, :py:meth:`Variable.load_async`.
Note that users are expected to limit concurrency themselves - xarray does not internally limit concurrency in any way.
(:issue:`10326`, :pull:`10327`) By `Tom Nicholas <https://github.com/TomNicholas>`_.
- :py:meth:`DataTree.to_netcdf` can now write to a file-like object, or return bytes if called without a filepath. (:issue:`10570`)
By `Matthew Willson <https://github.com/mjwillson>`_.
- Added exception handling for invalid files in :py:func:`open_mfdataset`. (:issue:`6736`)
Expand Down Expand Up @@ -50,12 +53,10 @@ Deprecations

Bug fixes
~~~~~~~~~

- Fix Pydap Datatree backend testing. Testing now compares elements of (unordered) two sets (before, lists) (:pull:`10525`).
By `Miguel Jimenez-Urias <https://github.com/Mikejmnez>`_.
- Fix ``KeyError`` when passing a ``dim`` argument different from the default to ``convert_calendar`` (:pull:`10544`).
By `Eric Jansen <https://github.com/ej81>`_.

- Fix transpose of boolean arrays read from disk. (:issue:`10536`)
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fix detection of the ``h5netcdf`` backend. Xarray now selects ``h5netcdf`` if the default ``netCDF4`` engine is not available (:issue:`10401`, :pull:`10557`).
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ dev = [
"pytest-mypy-plugins",
"pytest-timeout",
"pytest-xdist",
"pytest-asyncio",
"ruff>=0.8.0",
"sphinx",
"sphinx_autosummary_accessors",
Expand Down
7 changes: 7 additions & 0 deletions xarray/backends/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,17 @@ def robust_getitem(array, key, catch=Exception, max_retries=6, initial_delay=500
class BackendArray(NdimSizeLenMixin, indexing.ExplicitlyIndexed):
__slots__ = ()

async def async_getitem(self, key: indexing.ExplicitIndexer) -> np.typing.ArrayLike:
raise NotImplementedError("Backend does not not support asynchronous loading")

def get_duck_array(self, dtype: np.typing.DTypeLike = None):
key = indexing.BasicIndexer((slice(None),) * self.ndim)
return self[key] # type: ignore[index]

async def async_get_duck_array(self, dtype: np.typing.DTypeLike = None):
key = indexing.BasicIndexer((slice(None),) * self.ndim)
return await self.async_getitem(key)


class AbstractDataStore:
__slots__ = ()
Expand Down
50 changes: 50 additions & 0 deletions xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,23 @@ def encode_zarr_attr_value(value):
return encoded


def has_zarr_async_index() -> bool:
try:
import zarr

return hasattr(zarr.AsyncArray, "oindex")
except (ImportError, AttributeError):
return False


class ZarrArrayWrapper(BackendArray):
__slots__ = ("_array", "dtype", "shape")

def __init__(self, zarr_array):
# some callers attempt to evaluate an array if an `array` property exists on the object.
# we prefix with _ to avoid this inference.

# TODO type hint this?
self._array = zarr_array
self.shape = self._array.shape

Expand Down Expand Up @@ -213,6 +224,33 @@ def _vindex(self, key):
def _getitem(self, key):
return self._array[key]

async def _async_getitem(self, key):
if not _zarr_v3():
raise NotImplementedError(
"For lazy basic async indexing with zarr, zarr-python=>v3.0.0 is required"
)

async_array = self._array._async_array
return await async_array.getitem(key)

async def _async_oindex(self, key):
if not has_zarr_async_index():
raise NotImplementedError(
"For lazy orthogonal async indexing with zarr, zarr-python=>v3.1.2 is required"
)

async_array = self._array._async_array
return await async_array.oindex.getitem(key)

async def _async_vindex(self, key):
if not has_zarr_async_index():
raise NotImplementedError(
"For lazy vectorized async indexing with zarr, zarr-python=>v3.1.2 is required"
)

async_array = self._array._async_array
return await async_array.vindex.getitem(key)

def __getitem__(self, key):
array = self._array
if isinstance(key, indexing.BasicIndexer):
Expand All @@ -228,6 +266,18 @@ def __getitem__(self, key):
# if self.ndim == 0:
# could possibly have a work-around for 0d data here

async def async_getitem(self, key):
array = self._array
if isinstance(key, indexing.BasicIndexer):
method = self._async_getitem
elif isinstance(key, indexing.VectorizedIndexer):
method = self._async_vindex
elif isinstance(key, indexing.OuterIndexer):
method = self._async_oindex
return await indexing.async_explicit_indexing_adapter(
key, array.shape, indexing.IndexingSupport.VECTORIZED, method
)


def _determine_zarr_chunks(enc_chunks, var_chunks, ndim, name):
"""
Expand Down
3 changes: 3 additions & 0 deletions xarray/coding/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def __getitem__(self, key):
def get_duck_array(self):
return self.func(self.array.get_duck_array())

async def async_get_duck_array(self):
return self.func(await self.array.async_get_duck_array())

def __repr__(self) -> str:
return f"{type(self).__name__}({self.array!r}, func={self.func!r}, dtype={self.dtype!r})"

Expand Down
Loading
Loading