Skip to content

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

@nvaytet

Description

@nvaytet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions