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

Regression 0.14.0 -> 0.14.1: __setattr__ no longer updates matching index #3664

Closed
Huite opened this issue Jan 6, 2020 · 2 comments
Closed

Comments

@Huite
Copy link
Contributor

Huite commented Jan 6, 2020

MCVE Code Sample

This came up in the following (simplified) case:

import numpy as np
import xarray as xr

x = [1, 2, 3, 4]
data = [1., 2., 3. , 4.]
dims = ["x"]
coords = {"x": x}
da = xr.DataArray(data, coords, dims)
da1 = da.sel(x=[1, 3])
da2 = da.sel(x=[2, 4])
da2["x"].values = da1["x"].values
da3 = da2 - da1
print(da3)

In 0.14.1, this results in an empty DataArray da3:

<xarray.DataArray (x: 0)>
array([], dtype=float64)
Coordinates:
  * x        (x) int64 

0.14.0 (and before), no issues:

<xarray.DataArray (x: 2)>
array([1., 1.])
Coordinates:
  * x        (x) int32 1 3

Interestingly, the values of da["x"] are okay, print(da2["x"]") gives:
array([1, 3])

However, the index has not been updated, print(da2.indexes["x"]):
Int64Index([2, 4], dtype='int64', name='x')

Expected Output

I'm expecting output as in 0.14.0 and before. I've briefly stepped through the code for da2["x"].values = da1["x"].values; I don't see any changes there between 0.14.0 and 0.14.1, so I'm guessing it's due to some change in the indexes. Apparently the index no longer uses the same array?

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.7.5 (default, Oct 31 2019, 15:18:51) [MSC v.1916 64 bit (AMD64)] python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: en LOCALE: None.None libhdf5: 1.10.5 libnetcdf: 4.7.3

xarray: 0.14.1
pandas: 0.25.3
numpy: 1.17.3
scipy: 1.3.2
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.1
cfgrib: None
iris: None
bottleneck: None
dask: 2.6.0
distributed: 2.6.0
matplotlib: 3.1.1
cartopy: None
seaborn: None
numbagg: None
setuptools: 41.6.0.post20191030
pip: 19.3.1
conda: None
pytest: None
IPython: 7.9.0
sphinx: 2.2.1

@dcherian
Copy link
Contributor

dcherian commented Jan 6, 2020

Closing as dupe of #3470. :/ we really need a solution.

@dcherian dcherian closed this as completed Jan 6, 2020
@Huite
Copy link
Contributor Author

Huite commented Jan 6, 2020

Ah, thanks. Somehow I didn't search with quite the right search terms...

For posterity, the right way of doing this is by replacing:

da2["x"].values = da1["x"].values

by

da2 = da2.assign_coords(x=da1["x"])

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