pydata / pydata/xarray

decide on how to handle `empty_like`

Open
#7,940 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Is your feature request related to a problem?

calling np.empty_like seems to be instantiating the whole array.

from xarray.tests import InaccessibleArray
import xarray as xr
import numpy as np

array = InaccessibleArray(np.zeros((3, 3), dtype="uint8"))
da = xr.DataArray(array, dims=["x", "y"])

np.empty_like(da)
Traceback (most recent call last):
  File "/home/mark/t.py", line 8, in <module>
    np.empty_like(da)
  File "/home/mark/mambaforge/envs/dev/lib/python3.9/site-packages/xarray/core/common.py", line 165, in __array__
    return np.asarray(self.values, dtype=dtype)
  File "/home/mark/mambaforge/envs/dev/lib/python3.9/site-packages/xarray/core/dataarray.py", line 732, in values
    return self.variable.values
  File "/home/mark/mambaforge/envs/dev/lib/python3.9/site-packages/xarray/core/variable.py", line 614, in values
    return _as_array_or_item(self._data)
  File "/home/mark/mambaforge/envs/dev/lib/python3.9/site-packages/xarray/core/variable.py", line 314, in _as_array_or_item
    data = np.asarray(data)
  File "/home/mark/mambaforge/envs/dev/lib/python3.9/site-packages/xarray/tests/__init__.py", line 151, in __array__
    raise UnexpectedDataAccess("Tried accessing data")
xarray.tests.UnexpectedDataAccess: Tried accessing data
Describe the solution you'd like

I'm not too sure. This is why I raised this as a "feature" and not a bug.

On one hand, it is pretty hard to "get" the underlying class.

Is it a:

  • numpy array
  • a lazy thing that looks like a numpy array?
  • a dask array when it is dask?

I think that there are also some nuances between:

  1. Loading an nc file from a file (where things might be handled by dask even though you don't want them to be)
  2. Creating your xarray from in memory.
Describe alternatives you've considered

for now, i'm trying to avoid empty_like or zeros_like.

In general, we haven't seen much benefit from dask and cuda still needs careful memory management.

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read xarray/core/common.py, xarray/core/dataarray.py, and xarray/core/variable.py around the traceback, then reproduce the InaccessibleArray example. Determine the intended empty_like behavior for in-memory and lazy arrays; done means the behavior is agreed and covered by regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
backend-api-design, data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.