xarray.align can return read-only arrays
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
The attached code snippet raises "ValueError: output array is read-only" because the d1 array returned from xarray.align is read-only.
What did you expect to happen?
d1 should not be a read-only. d2 should have been added to it without issue.
Minimal Complete Verifiable Example
import numpy as np
import xarray as xr
d1 = xr.DataArray(np.array([]), coords={'dim': []})
d2 = xr.DataArray(np.array([1]), coords={'dim': ['x']})
d1 , d2 = xr.align(d1, d2, join='outer', fill_value=0, copy=True)
d1 += d2
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
Relevant log output
Traceback (most recent call last):
File "/home/atest.py", line 7, in <module>
d1 += d2
File "/some/where/lib/python3.10/site-packages/xarray/core/_typed_ops.py", line 328, in __iadd__
return self._inplace_binary_op(other, operator.iadd)
File "/some/where/lib/python3.10/site-packages/xarray/core/dataarray.py", line 4659, in _inplace_binary_op
f(self.variable, other_variable)
File "/some/where/lib/python3.10/site-packages/xarray/core/_typed_ops.py", line 680, in __iadd__
return self._inplace_binary_op(other, operator.iadd)
File "/some/where/lib/python3.10/site-packages/xarray/core/variable.py", line 2710, in _inplace_binary_op
self.values = f(self_data, other_data)
ValueError: output array is read-only
Anything else we need to know?
I suspect this is related to https://github.com/pydata/xarray/issues/3813, i.e. that align is calling expand_dims behind the scenes. But even if the expand_dims aspect is considered a feature rather than a bug, the DataArray.copy() required to fix it should be done by align since the documentation says it should return a copy, not a view.
Environment
INSTALLED VERSIONS
commit: None
python: 3.10.10 (main, Feb 9 2023, 14:42:48) [GCC 8.5.0 20210514 (Red Hat 8.5.0-10)]
python-bits: 64
OS: Linux
OS-release: 4.18.0-372.52.1.el8_6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: ('en_GB', 'UTF-8')
libhdf5: None
libnetcdf: None
xarray: 2023.9.0
pandas: 2.1.1
numpy: 1.26.0
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 65.5.0
pip: 22.3.1
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the MVCE with the reported xarray 2023.9.0 environment, then trace the aligned result through the reported xarray/core/dataarray.py, xarray/core/variable.py, and _typed_ops.py paths. Add a regression test showing that align(..., copy=True) returns writable data and that d1 += d2 succeeds.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100