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..b061ecb1 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( @@ -1377,6 +1385,11 @@ class DelegatedScalarMethod(DelegatedMethod): to_series = False +class DelegatedInplaceMethod(DelegatedMethod): + to_series = False + reinitialize = True + + for attr in [ "debug_used", "dimensionality", @@ -1395,16 +1408,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",