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

dataarray arithmetics restore removed coordinates in xarray 0.15 #3746

Closed
mraspaud opened this issue Feb 4, 2020 · 5 comments · Fixed by #3840
Closed

dataarray arithmetics restore removed coordinates in xarray 0.15 #3746

mraspaud opened this issue Feb 4, 2020 · 5 comments · Fixed by #3840
Labels

Comments

@mraspaud
Copy link
Contributor

mraspaud commented Feb 4, 2020

MCVE Code Sample

import xarray as xr
import numpy as np
arr2 = xr.DataArray(np.ones((2, 2)), dims=['y', 'x'])
arr1 = xr.DataArray(np.ones((2, 2)), dims=['y', 'x'], coords={'y': [0, 1], 'x': [0, 1]})

del arr1.coords['y']
del arr1.coords['x']

# shows arr1 without coordinates
arr1

# shows coordinates in xarray 0.15
arr1 * arr2

Expected Output

<xarray.DataArray (y: 2, x: 2)>
array([[1., 1.],
       [1., 1.]])
Dimensions without coordinates: y, x

Problem Description

In xarray 0.15, the coordinates are restored when doing the multiplication:

<xarray.DataArray (y: 2, x: 2)>
array([[1., 1.],
       [1., 1.]])
Coordinates:
  * y        (y) int64 0 1
  * x        (x) int64 0 1

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.1 | packaged by conda-forge | (default, Jan 29 2020, 14:55:04)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 4.18.0-147.0.3.el8_1.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.3

xarray: 0.15.0
pandas: 1.0.0
numpy: 1.18.1
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: 0.7.4
h5py: 2.10.0
Nio: None
zarr: 2.3.2
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.2
cfgrib: None
iris: None
bottleneck: 1.3.1
dask: 2.10.1
distributed: 2.10.0
matplotlib: 3.1.3
cartopy: 0.17.0
seaborn: None
numbagg: None
setuptools: 45.1.0.post20200119
pip: 20.0.2
conda: None
pytest: 5.3.5
IPython: 7.12.0
sphinx: 2.3.1

@keewis
Copy link
Collaborator

keewis commented Feb 4, 2020

the issue here is that if you call __delitem__ on DataArray.coords (which returns a dict-like object named DataArrayCoordinates), it deletes the coordinates, but leaves the indexes intact:

In [13]: arr1_ = arr1.copy()
    ...: del arr1_.coords["x"]
    ...: del arr1_.coords["y"]
    ...: arr1_.coords, arr1_.indexes
Out[13]:
(Coordinates:
     *empty*, y: Int64Index([0, 1], dtype='int64', name='y')
 x: Int64Index([0, 1], dtype='int64', name='x'))

Since #3481 the indexes are passed along in binary operations (such as multiplication) and will thus cause the coordinates reappear. Note that coordinates without indexes (non-dimension coordinates) will be removed properly.

If we want to support this way of dropping coordinates we should update the indexes, explicitly or via drop_vars, otherwise we might want to make *Coordinates objects read-only.

For now the easiest way to properly remove coordinates is by using drop_vars.

@mraspaud
Copy link
Contributor Author

mraspaud commented Feb 4, 2020

@keewis thanks for the quick reply. I wasn't aware the builtin del wasn't supposed to work here, I'll try with drop_vars instead.

@keewis
Copy link
Collaborator

keewis commented Feb 4, 2020

I don't know whether or not this was supposed to work. Since there is a explicit __delitem__, I'd say it was at some point and my question above is more along the lines of "should this still work?".

@mraspaud
Copy link
Contributor Author

mraspaud commented Feb 4, 2020

Thanks for the clarification. I can confirm that drop_vars works as expected. As a user, I would vote for having __delitem__ call drop_vars, at least for now to keep backwards compatibility ?

@dcherian
Copy link
Contributor

dcherian commented Feb 4, 2020

IMO we should fix to remove associated indexes and issue a patch release.

@dcherian dcherian added the bug label Feb 4, 2020
dcherian added a commit to dcherian/xarray that referenced this issue Mar 6, 2020
max-sixty pushed a commit that referenced this issue Mar 21, 2020
* Delete associated indexes when deleting coordinate variables.

Fixes #3746

* review

* fix tests
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.

3 participants