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

Dask now supports item assignment. Shouldn't xarray support it too? #5171

Closed
tammasloughran opened this issue Apr 16, 2021 · 2 comments
Closed

Comments

@tammasloughran
Copy link
Contributor

As of a few weeks ago, dask now supports item assignment. Documentation here.

At the moment, xarray prevents item assignment if a DataArray uses dask arrays.

>>> import xarray as xr
>>> foo = xr.DataArray([1,2,3,4,5,6], dims=('x'), coords={'x':[1,2,3,4,5,6]}).chunk(2)
>>> foo.loc[1] = 44
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tammas/miniconda3/envs/da/lib/python3.8/site-packages/xarray/core/dataarray.py", line 213, in __setitem__
    self.data_array[pos_indexers] = value
  File "/home/tammas/miniconda3/envs/da/lib/python3.8/site-packages/xarray/core/dataarray.py", line 724, in __setitem__
    self.variable[key] = value
  File "/home/tammas/miniconda3/envs/da/lib/python3.8/site-packages/xarray/core/variable.py", line 889, in __setitem__
    indexable[index_tuple] = value
  File "/home/tammas/miniconda3/envs/da/lib/python3.8/site-packages/xarray/core/indexing.py", line 1379, in __setitem__
    raise TypeError(
TypeError: this variable's data is stored in a dask array, which does not support item assignment. To assign to this variable, you must first load it into memory explicitly using the .load() method or accessing its .values attribute.

Now that this is possible in dask, shouldn't xarray allow it too if the installed dask version supports it?

Kind regards,
Tam

@max-sixty
Copy link
Collaborator

Yes! We'd definitely take a PR for this.

Related — we can also speed up .stack for dask arrays, which we currently have a workaround for because of this.

@tammasloughran
Copy link
Contributor Author

PR #5174 . This is a start. I'm not too familiar with the inner workings of xarray, so it might need more work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants