Skip to content

add multiindex level name checking to .rename() #3658

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
@@ -62,6 +62,8 @@ Bug fixes
By `Tom Augspurger <https://github.com/TomAugspurger>`_.
- Ensure :py:meth:`Dataset.quantile`, :py:meth:`DataArray.quantile` issue the correct error
when ``q`` is out of bounds (:issue:`3634`) by `Mathias Hauser <https://github.com/mathause>`_.
- :py:meth:`Dataset.rename`, :py:meth:`DataArray.rename` now check for conflicts with
MultiIndex level names.

Documentation
~~~~~~~~~~~~~
9 changes: 8 additions & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
@@ -89,7 +89,13 @@
is_scalar,
maybe_wrap_array,
)
from .variable import IndexVariable, Variable, as_variable, broadcast_variables
from .variable import (
IndexVariable,
Variable,
as_variable,
broadcast_variables,
assert_unique_multiindex_level_names,
)

if TYPE_CHECKING:
from ..backends import AbstractDataStore, ZarrStore
@@ -2780,6 +2786,7 @@ def rename(
variables, coord_names, dims, indexes = self._rename_all(
name_dict=name_dict, dims_dict=name_dict
)
assert_unique_multiindex_level_names(variables)
return self._replace(variables, coord_names, dims=dims, indexes=indexes)

def rename_dims(
8 changes: 8 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
@@ -2461,6 +2461,14 @@ def test_rename_vars(self):
with pytest.raises(ValueError):
original.rename_vars(names_dict_bad)

def test_rename_multiindex(self):
mindex = pd.MultiIndex.from_tuples(
[([1, 2]), ([3, 4])], names=["level0", "level1"]
)
data = Dataset({}, {"x": mindex})
with raises_regex(ValueError, "conflicting MultiIndex"):
data.rename({"x": "level0"})

@requires_cftime
def test_rename_does_not_change_CFTimeIndex_type(self):
# make sure CFTimeIndex is not converted to DatetimeIndex #3522