From 32dd2553d9a40695f820830b82bd97c436e6ac2c Mon Sep 17 00:00:00 2001 From: Etienne Bresciani <53166244+etiennebresciani@users.noreply.github.com> Date: Wed, 13 Aug 2025 10:45:53 -0400 Subject: [PATCH 1/2] Fix inplace accessor methods (#282) - Fix inplace accessor methods (ito(), etc.; Issue #282) - Fix corresponding tests, which were wrong --- CHANGES | 1 + pint_pandas/pint_array.py | 23 +++++++++++++++---- .../testsuite/test_pandas_interface.py | 5 ++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index e7f7e063..1362ef0f 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,7 @@ pint-pandas Changelog - `Dataframe.pint.quantify` and `Dataframe.pint.dequantify` will now accept and return single row headers, allowing it to parse column names such as `torque [lbf in]`. +- Fix inplace accessor methods (ito(), etc.; Issue #282) 0.7.1 (2025-01-06) -------------------- diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index 3468c8c2..af3c2f16 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -1355,6 +1355,8 @@ class DelegatedScalarProperty(DelegatedProperty): class DelegatedMethod(Delegated): + reinitialize = False + def __get__(self, obj, type=None): index = object.__getattribute__(obj, "_index") name = object.__getattribute__(obj, "_name") @@ -1362,6 +1364,12 @@ def __get__(self, obj, type=None): def delegated_method(*args, **kwargs): result = method(*args, **kwargs) + if self.reinitialize: + q = object.__getattribute__(obj, "quantity") + values = pd.array(q.m) + dtype = PintType(units=q.units, subdtype=ddtypemap[q.dtype]) + po = object.__getattribute__(obj, "pandas_obj") + po.__init__(values, dtype=dtype) if self.to_series: if isinstance(result, _Quantity): result = PintArray.from_1darray_quantity( @@ -1376,6 +1384,10 @@ def delegated_method(*args, **kwargs): class DelegatedScalarMethod(DelegatedMethod): to_series = False +class DelegatedInplaceMethod(DelegatedMethod): + to_series = False + reinitialize = True + for attr in [ "debug_used", @@ -1395,16 +1407,19 @@ class DelegatedScalarMethod(DelegatedMethod): "check", "compatible_units", "format_babel", + "plus_minus", + "to_tuple", + "tolist", +]: + setattr(PintSeriesAccessor, attr, DelegatedScalarMethod(attr)) +for attr in [ "ito", "ito_base_units", "ito_reduced_units", "ito_root_units", - "plus_minus", "put", - "to_tuple", - "tolist", ]: - setattr(PintSeriesAccessor, attr, DelegatedScalarMethod(attr)) + setattr(PintSeriesAccessor, attr, DelegatedInplaceMethod(attr)) for attr in [ "clip", "from_tuple", diff --git a/pint_pandas/testsuite/test_pandas_interface.py b/pint_pandas/testsuite/test_pandas_interface.py index 858210be..86bad41a 100644 --- a/pint_pandas/testsuite/test_pandas_interface.py +++ b/pint_pandas/testsuite/test_pandas_interface.py @@ -377,8 +377,9 @@ def test_series_inplace_method_accessors(self, data, attr_args): s = pd.Series(deepcopy(data)) getattr(s.pint, attr)(*args) - getattr(data.quantity, attr)(*args) - assert all(s.values == data) + q = data.quantity + getattr(q, attr)(*args) + assert all(s.values == q) @pytest.mark.parametrize( "attr_args", From f56641ba1f2c57834e5a45808629e0ad52067d35 Mon Sep 17 00:00:00 2001 From: Etienne Bresciani <53166244+etiennebresciani@users.noreply.github.com> Date: Wed, 13 Aug 2025 10:59:17 -0400 Subject: [PATCH 2/2] Fix code format Fix code format (missing blank line) --- pint_pandas/pint_array.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index af3c2f16..b061ecb1 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -1384,6 +1384,7 @@ def delegated_method(*args, **kwargs): class DelegatedScalarMethod(DelegatedMethod): to_series = False + class DelegatedInplaceMethod(DelegatedMethod): to_series = False reinitialize = True