Skip to content

.rename() does not check conflict with multiindex level names #3657

Closed
@hazbottles

Description

@hazbottles

MCVE Code Sample

>>> import xarray as xr
>>> ds = xr.Dataset({"x": 42}, {"level0": [0, 1], "level1": [3, 4]}).set_index(mindex=["level0", "level1"])
>>> ds
<xarray.Dataset>
Dimensions:  (mindex: 2)
Coordinates:
  * mindex   (mindex) MultiIndex
  - level0   (mindex) int64 0 1
  - level1   (mindex) int64 3 4
Data variables:
    x        int64 42
>>> ds.rename({"x": "level0"})
<xarray.Dataset>
Dimensions:  (mindex: 2)
Coordinates:
  * mindex   (mindex) MultiIndex
  - level0   (mindex) int64 0 1
  - level1   (mindex) int64 3 4
Data variables:
    level0   int64 42

Expected Output

raise a ValueError("conflicting Multiindex names ...")

Problem Description

Using .rename() you can get into a situation where you have a data variable or coordinate with the same name as a multi-index level name.

Output of xr.show_versions()

INSTALLED VERSIONS

commit: b3d3b44
python: 3.8.0 | packaged by conda-forge | (default, Nov 22 2019, 19:11:38)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 4.4.0-18362-Microsoft
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: None
libnetcdf: None

xarray: 0.14.1+36.gb3d3b44
pandas: 0.25.3
numpy: 1.18.0
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.9.1
distributed: 2.9.1
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
setuptools: 42.0.2.post20191201
pip: 19.3.1
conda: None
pytest: 5.3.2
IPython: None
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions