Skip to content

Commit 8c48f25

Browse files
authored
properly rename the coordinate units (#351)
* expect to have renamed the coordinate * also rename the units * changelog
1 parent d248a1d commit 8c48f25

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

docs/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ What's new
44
==========
55
0.6.1 (*unreleased*)
66
--------------------
7-
7+
- Properly rename the coordinate units (:pull:`351`).
8+
By `Justus Magin <https://github.com/keewis>`_.
89

910
0.6.0 (31 Aug 2025)
1011
-------------------

pint_xarray/index.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ def roll(self, shifts):
112112
return self._replace(self.index.roll(shifts))
113113

114114
def rename(self, name_dict, dims_dict):
115-
return self._replace(self.index.rename(name_dict, dims_dict))
115+
new_units = {new: self.units[old] for old, new in name_dict.items()}
116+
return self.__class__(
117+
index=self.index.rename(name_dict, dims_dict), units=new_units
118+
)
116119

117120
def __getitem__(self, indexer):
118121
return self._replace(self.index[indexer])

pint_xarray/tests/test_index.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,12 @@ def test_roll(shifts, expected_index):
260260
@pytest.mark.parametrize("name_dict", ({"y2": "y3"}, {"y2": "y1"}))
261261
def test_rename(name_dict, dims_dict):
262262
wrapped_index = PandasIndex(pd.Index([1, 2], name="y2"), dim="y")
263-
index = PintIndex(index=wrapped_index, units={"y": ureg.Unit("m")})
263+
index = PintIndex(index=wrapped_index, units={"y2": ureg.Unit("m")})
264264

265265
actual = index.rename(name_dict, dims_dict)
266266
expected = PintIndex(
267-
index=wrapped_index.rename(name_dict, dims_dict), units=index.units
267+
index=wrapped_index.rename(name_dict, dims_dict),
268+
units={name_dict["y2"]: ureg.Unit("m")},
268269
)
269270

270271
assert actual.equals(expected)

0 commit comments

Comments
 (0)