hgrecco / pint-pandas

Pandas support for pint
Other
172 stars 42 forks source link

Changing Unit Without Applying Conversion #223

Closed Musaefendic closed 5 months ago

Musaefendic commented 5 months ago

Context

I am performing a calculation using an empirical formula. pint-pandas assumes that the unit of the calculation is dimensionless, but I want to force the unit to g/kg, to comply with the empirical formula.

I recreate a pd.Series with the target unit, but pint-pandas applies a factor 1000 ratio that seems to be related to the grams unit.

Question: I might have missed something in the documentation; is this the normal behavior?

Environment

I'm not using the latest versions, due to a Python=3.8 constraint. I have not yet tested with the latest versions.

python=3.8
pandas=2.03
pint=0.20
pint-pandas=0.3

Minimal Reproducible Example

data = pd.DataFrame(
    {
        'CO2': pd.Series([.2], dtype='pint[percent]'),
        'CO': pd.Series([6.], dtype='pint[ppm]'),
    }
)

CO2 = data['CO2'].pint.to('')
CO = data['CO'].pint.to('')

result = 1000. * 50. * CO / 10. * CO2

print('\n> EXPECTATION - Calculation by hand')
print(result)

print('\n> RESULT with pint-pandas - WRONG order of magnitude')
print(pd.Series(result, dtype='pint[g/kg]'))
> EXPECTATION - Calculation by hand
0    6e-05
dtype: pint[dimensionless]

> RESULT with pint-pandas - WRONG order of magnitude
0    6e-02
dtype: pint[gram / kilogram]```

Workaround

pd.Series(result.pint.magnitude, dtype='pint[g/kg]')  # drop unit with .pint.magnitude
andrewgsavage commented 5 months ago

that looks correct, pd.Series(result, dtype='pint[g/kg]') tells pint-pandas to convert result from [dimensionless] to [g/kg], so it multiplies by 1e3

you could: