hgrecco / pint-pandas

Pandas support for pint
Other
173 stars 42 forks source link

clip() does not work on pint-typed columns #216

Closed mkaut closed 5 months ago

mkaut commented 9 months ago

Using clip on pint-typed columns issues UnitStrippedWarning with Pint-Pandas 0.5, Pint 0.23 and pandas 2.1.4, on Windows. For example:

import pandas as pd
import pint_pandas

ureg = pint_pandas.PintType.ureg
lb = ureg.Quantity(2, 'lbf ft')

df = pd.DataFrame({
    "torque": pd.Series([1, 2, 2, 3], dtype="pint[lbf ft]"),
    "angular_velocity": pd.Series([1, 2, 2, 3], dtype="pint[rpm]"),
})
print(df['torque'].clip(lower=lb))

results in

<MY_PATH>\Lib\site-packages\pandas\core\dtypes\missing.py:225: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.
  return _isna_array(np.asarray(obj), inf_as_na=inf_as_na)
0    2
1    2
2    2
3    3
Name: torque, dtype: pint[foot * force_pound]

It calculates the correct values, but issues a warning.

I have also tested using a non-typed value as the bound and that results in an error (as it should) - except for 0, which for some reason works and does not issue the warning.

andrewgsavage commented 9 months ago

I don't get that warning using pint master. I thnik https://github.com/hgrecco/pint/pull/1909 removes the warning, so it'll go when the next pint version is released.