pydata / pydata/xarray

Xarray operations produce read-only array

Open
#3,813 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I've turned on testing my Satpy package with unstable or pre-releases of some of our dependencies including numpy and xarray. I've found one error so far where in previous versions of xarray it was possible to assign to the numpy array taken from a DataArray.

MCVE Code Sample
import numpy as np
import dask.array as da
import xarray as xr

data = np.arange(15, 301, 15).reshape(2, 10)
data_arr = xr.DataArray(data, dims=('y', 'x'), attrs={'test': 'test'})
data_arr = data_arr.copy()
data_arr = data_arr.expand_dims('bands')
data_arr['bands'] = ['L']
n_arr = np.asarray(data_arr.data)
n_arr[n_arr == 45] = 5

Which results in:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-90dae37dd808> in <module>
----> 1 n_arr = np.asarray(data_arr.data); n_arr[n_arr == 45] = 5

ValueError: assignment destination is read-only
Expected Output

A writable array. No error.

Problem Description

If this is expected new behavior then so be it, but wanted to check with the xarray devs before I tried to work around it.

Output of xr.show_versions()
INSTALLED VERSIONS ------------------ commit: None python: 3.7.6 | packaged by conda-forge | (default, Jan 7 2020, 22:33:48) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: 5.3.0-7629-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8 libhdf5: 1.10.5 libnetcdf: 4.7.3

xarray: 0.15.1.dev21+g20e6236f
pandas: 1.1.0.dev0+630.gedcf1c8f8
numpy: 1.19.0.dev0+acba244
scipy: 1.5.0.dev0+f614064
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: 2.4.0
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.3
cfgrib: None
iris: None
bottleneck: None
dask: 2.11.0+13.gfcc500c2
distributed: 2.11.0+7.g0d7a31ad
matplotlib: 3.2.0rc3
cartopy: 0.17.0
seaborn: None
numbagg: None
setuptools: 45.2.0.post20200209
pip: 20.0.2
conda: None
pytest: 5.3.5
IPython: 7.12.0
sphinx: 2.4.3

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

Start with the MCVE in the issue and trace how DataArray.data is produced after copy(), expand_dims(), and coordinate assignment. Check the relevant xarray and NumPy behavior to determine whether the read-only result is intentional; done means resolving or clearly documenting the expected writability and covering the reproduced case with a regression test.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.