hgrecco / pint-pandas

Pandas support for pint
Other
166 stars 40 forks source link

Fix issue where quantify changes the type of arrays to obj if the DataFrame containes string columns #225

Closed thaeber closed 2 months ago

thaeber commented 2 months ago

217 only seems to fix the unwanted type conversion to dtype='object' for columns with units, while the same happens with numeric columns without units.

In the following example, the "something_else" column has numerical values, but no associated units:

import pandas as pd
import pint_pandas

df = pd.DataFrame(
    {
        'power': pd.Series([1.0, 2.0, 3.0], dtype='pint[W]'),
        'torque': pd.Series([4.0, 5.0, 6.0], dtype='pint[N*m]'),
        'fruits': pd.Series(['apple', 'pear', 'kiwi']),
        'something_else': pd.Series([7.0, 8.0, 9.0]),
    }
)
df1 = df.pint.dequantify().pint.quantify(level=-1)

print(df.dtypes)

print(df1.dtypes)

1st print statement: grafik

2nd print statement: grafik

This is fixed by using iloc, both, for columns with and without units.

andrewgsavage commented 2 months ago

nice one