-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Dropping a MultiIndex variable raises an error after explicit indexes refactor #6505
Comments
I agree a depreciation warning is a bit nicer than an error message for this specific case. If we agree on eventually removing the pandas multi-index dimension coordinate, perhaps this issue should be addressed in that wider context as it will be directly impacted? It would make our plans clear to users if we issue a depreciation warning message that already mentions this future removal (and that we could also issue now or later in other places like |
Here's another version of the same bug affecting `Dataset.assign) import xarray
array = xarray.DataArray(
[[1, 2], [3, 4]],
dims=['x', 'y'],
coords={'x': ['a', 'b']},
)
stacked = array.stack(z=['x', 'y']).to_dataset(name="a")
stacked.assign_coords(f1=2*stacked.a) # works
stacked.assign(f2=2*stacked.a) # error
```
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [33], in ()
8 stacked = array.stack(z=['x', 'y']).to_dataset(name="a")
9 stacked.assign_coords(f1=2*stacked.a)
---> 10 stacked.assign(f2=2*stacked.a)
File ~/work/python/xarray/xarray/core/dataset.py:5444, in Dataset.assign(self, variables, **variables_kwargs) File ~/work/python/xarray/xarray/core/dataset.py:4421, in Dataset.update(self, other) File ~/work/python/xarray/xarray/core/merge.py:1068, in dataset_update_method(dataset, other) File ~/work/python/xarray/xarray/core/dataarray.py:2406, in DataArray.drop_vars(self, names, errors) File ~/work/python/xarray/xarray/core/dataset.py:4549, in Dataset.drop_vars(self, names, errors) File ~/work/python/xarray/xarray/core/indexes.py:1394, in assert_no_index_corrupted(indexes, coord_names) ValueError: cannot remove coordinate(s) 'y', 'x', which would corrupt the following index built from coordinates 'z', 'x', 'y':
|
Reopening because my second example |
What happened?
With the latest released version of Xarray, it is possible to delete all variables corresponding to a MultiIndex by simply deleting the name of the MultiIndex.
After the explicit indexes refactor (i.e,. using the "main" development branch) this now raises error about how this would "corrupt" index state. This comes up when using
drop()
andassign_coords()
and possibly some other methods.This is not hard to work around, but we may want to consider this bug a blocker for the next Xarray release. I found the issue surfaced in several projects when attempting to use the new version of Xarray inside Google's codebase.
CC @benbovy in case you have any thoughts to share.
What did you expect to happen?
For now, we should preserve the behavior of deleting the variables corresponding to MultiIndex levels, but should issue a deprecation warning encouraging users to explicitly delete everything.
Minimal Complete Verifiable Example
Relevant log output
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: 33cdabd
python: 3.10.4 | packaged by conda-forge | (main, Mar 24 2022, 17:42:03) [Clang 12.0.1 ]
python-bits: 64
OS: Darwin
OS-release: 21.4.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: 4.8.1
xarray: 0.18.3.dev137+g96c56836
pandas: 1.4.2
numpy: 1.22.3
scipy: 1.8.0
netCDF4: 1.5.8
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.11.3
cftime: 1.6.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2022.04.1
distributed: 2022.4.1
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: 2022.3.0
cupy: None
pint: None
sparse: None
setuptools: 62.1.0
pip: 22.0.4
conda: None
pytest: 7.1.1
IPython: 8.2.0
sphinx: None
The text was updated successfully, but these errors were encountered: