Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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)
--------------------
Expand Down
24 changes: 20 additions & 4 deletions pint_pandas/pint_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,13 +1355,21 @@ class DelegatedScalarProperty(DelegatedProperty):


class DelegatedMethod(Delegated):
reinitialize = False

def __get__(self, obj, type=None):
index = object.__getattribute__(obj, "_index")
name = object.__getattribute__(obj, "_name")
method = getattr(object.__getattribute__(obj, "quantity"), self.name)

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(
Expand All @@ -1377,6 +1385,11 @@ class DelegatedScalarMethod(DelegatedMethod):
to_series = False


class DelegatedInplaceMethod(DelegatedMethod):
to_series = False
reinitialize = True


for attr in [
"debug_used",
"dimensionality",
Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions pint_pandas/testsuite/test_pandas_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading