Skip to content

Commit

Permalink
Showing 3 changed files with 18 additions and 1 deletion.
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

0 comments on commit db36c5c

Please sign in to comment.