Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ Documentation
Internal Changes
~~~~~~~~~~~~~~~~

- Skip the ``to_duck_array`` dispatch in :py:meth:`Variable.load` and
:py:meth:`Variable.load_async` when the underlying data is already a
``numpy.ndarray``. The dispatch was a no-op for that case but added
noticeable per-variable overhead to :py:meth:`Dataset.load` /
:py:meth:`DataArray.load` on datasets with many in-memory variables
(:issue:`11352`).


.. _whats-new.2026.04.0:

Expand Down
7 changes: 7 additions & 0 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,11 @@ def load(self, **kwargs) -> Self:
DataArray.load
Dataset.load
"""
# Fast path: an in-memory numpy ndarray has nothing to load. Subclasses
# that advertise a `chunks` attribute (test fakes, third-party chunked
# ndarray subclasses) must still go through to_duck_array.
if isinstance(self._data, np.ndarray) and not hasattr(self._data, "chunks"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this in to to_duck_array and async_to_duck_array instead

return self
self._data = to_duck_array(self._data, **kwargs)
return self

Expand Down Expand Up @@ -1052,6 +1057,8 @@ async def load_async(self, **kwargs) -> Self:
DataArray.load_async
Dataset.load_async
"""
if isinstance(self._data, np.ndarray) and not hasattr(self._data, "chunks"):
return self
self._data = await async_to_duck_array(self._data, **kwargs)
return self

Expand Down
Loading