diff --git a/docs/whats-new.rst b/docs/whats-new.rst index e91dae9e..a00ba543 100644 --- a/docs/whats-new.rst +++ b/docs/whats-new.rst @@ -4,7 +4,8 @@ What's new ========== 0.6.1 (*unreleased*) -------------------- - +- Properly rename the coordinate units (:pull:`351`). + By `Justus Magin `_. 0.6.0 (31 Aug 2025) ------------------- diff --git a/pint_xarray/index.py b/pint_xarray/index.py index 7c233f3e..db031f83 100644 --- a/pint_xarray/index.py +++ b/pint_xarray/index.py @@ -112,7 +112,10 @@ def roll(self, shifts): return self._replace(self.index.roll(shifts)) def rename(self, name_dict, dims_dict): - return self._replace(self.index.rename(name_dict, dims_dict)) + new_units = {new: self.units[old] for old, new in name_dict.items()} + return self.__class__( + index=self.index.rename(name_dict, dims_dict), units=new_units + ) def __getitem__(self, indexer): return self._replace(self.index[indexer]) diff --git a/pint_xarray/tests/test_index.py b/pint_xarray/tests/test_index.py index 481fe7a5..1c9f6cf0 100644 --- a/pint_xarray/tests/test_index.py +++ b/pint_xarray/tests/test_index.py @@ -260,11 +260,12 @@ def test_roll(shifts, expected_index): @pytest.mark.parametrize("name_dict", ({"y2": "y3"}, {"y2": "y1"})) def test_rename(name_dict, dims_dict): wrapped_index = PandasIndex(pd.Index([1, 2], name="y2"), dim="y") - index = PintIndex(index=wrapped_index, units={"y": ureg.Unit("m")}) + index = PintIndex(index=wrapped_index, units={"y2": ureg.Unit("m")}) actual = index.rename(name_dict, dims_dict) expected = PintIndex( - index=wrapped_index.rename(name_dict, dims_dict), units=index.units + index=wrapped_index.rename(name_dict, dims_dict), + units={name_dict["y2"]: ureg.Unit("m")}, ) assert actual.equals(expected)