Skip to content

In-place operation on DataArray coordinate throws error but still modifies the coordinate values #5036

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

Closed
nvaytet opened this issue Mar 15, 2021 · 1 comment · Fixed by #5957
Labels

Comments

@nvaytet
Copy link

nvaytet commented Mar 15, 2021

Example:

import xarray as xr
import numpy as np
data = np.random.rand(4, 3)
locs = np.arange(3.)
times = np.arange(4.)
foo = xr.DataArray(data, coords=[times, locs], dims=["time", "space"])
foo

Screenshot at 2021-03-15 13-47-22

When trying to do an in-place modification of the coordinate values

foo.coords['time'] += 10.

I get

ValueError: Cannot assign to the .values attribute of dimension coordinate a.k.a IndexVariable 'time'. Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate.

However, when looking at the values of the coordinate, they seem to have been modified by the operation:
Screenshot at 2021-03-15 13-49-12

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.7 (default, May 7 2020, 21:25:33)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-66-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.6
libnetcdf: None

xarray: 0.16.2
pandas: 1.2.0
numpy: 1.19.4
scipy: 1.5.3
netCDF4: None
pydap: None
h5netcdf: None
h5py: 3.1.0
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.2.2
cartopy: None
seaborn: None
numbagg: None
pint: 0.16.1
setuptools: 49.6.0.post20201009
pip: 20.3.3
conda: 4.9.2
pytest: 6.1.2
IPython: 7.19.0
sphinx: 3.3.0

@shoyer shoyer added the bug label Mar 16, 2021
@shoyer
Copy link
Member

shoyer commented Mar 16, 2021

Thanks for the report!

This definitely seems like a bug. I confirmed that it is still present in xarray v0.17.

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

Successfully merging a pull request may close this issue.

2 participants