From e2386b4588cec6459158d265208194d1fb3055bf Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Thu, 5 Feb 2026 00:41:34 +0100 Subject: [PATCH 1/3] avoid nesting `PintIndex` instances --- pint_xarray/conversion.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pint_xarray/conversion.py b/pint_xarray/conversion.py index ecda7af3..4849b1d7 100644 --- a/pint_xarray/conversion.py +++ b/pint_xarray/conversion.py @@ -137,10 +137,13 @@ def attach_units_index(index, index_vars, units): # skip non-quantity indexed variables return index - if isinstance(index, PintIndex) and index.units != units: - raise ValueError( - f"cannot attach units to quantified index: {index.units} != {units}" - ) + if isinstance(index, PintIndex): + if index.units != units: + raise ValueError( + f"cannot attach units to quantified index: {index.units} != {units}" + ) + else: + return index return PintIndex(index=index, units=units) From 095743b87a966f03c8cc68f971aa0963db88fc97 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Thu, 5 Feb 2026 00:41:55 +0100 Subject: [PATCH 2/3] correctly compute the index units --- pint_xarray/tests/test_conversion.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pint_xarray/tests/test_conversion.py b/pint_xarray/tests/test_conversion.py index f1d8dde0..057e6b18 100644 --- a/pint_xarray/tests/test_conversion.py +++ b/pint_xarray/tests/test_conversion.py @@ -418,11 +418,12 @@ def test_convert_units(self, type, variant, units, error, suberrors): expected_a = convert_quantity(q_a, units.get("a", original_units.get("a"))) expected_b = convert_quantity(q_b, units.get("b", original_units.get("b"))) expected_u = convert_quantity(q_u, units.get("u", original_units.get("u"))) - expected_x = convert_quantity(q_x, units.get("x")) + expected_x = convert_quantity(q_x, units.get("x", original_units.get("x"))) expected_index = PandasIndex(pd.Index(strip_quantity(expected_x)), "x") - if units.get("x") is not None: + expected_index_units = units.get("x", original_units.get("x")) + if expected_index_units is not None: expected_index = PintIndex( - index=expected_index, units={"x": units.get("x")} + index=expected_index, units={"x": expected_index_units} ) expected = Dataset( From ad5c4de5f00e4279693de51b0d1c4f45163de8e4 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Thu, 5 Feb 2026 00:50:41 +0100 Subject: [PATCH 3/3] whats-new [skip-ci][test-upstream] --- docs/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/whats-new.rst b/docs/whats-new.rst index 48cebe3c..44e4733b 100644 --- a/docs/whats-new.rst +++ b/docs/whats-new.rst @@ -8,6 +8,8 @@ What's new By `Justus Magin `_. - Don't rely on ``xarray``'s default behavior regarding ``attrs`` in :py:func:`pint_xarray.expects` (:issue:`360`, :pull:`367`) By `Justus Magin `_. +- Fix quantifying a existing index and adjust the index conversion tests (:pull:`368`) + By `Justus Magin `_. 0.6.0 (31 Aug 2025) -------------------