Skip to content

RTFM and could not make pint[Mt CO2] work as a unit #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MichaelTiemannOSC opened this issue Nov 2, 2021 · 4 comments
Closed

RTFM and could not make pint[Mt CO2] work as a unit #32

MichaelTiemannOSC opened this issue Nov 2, 2021 · 4 comments

Comments

@MichaelTiemannOSC
Copy link

MichaelTiemannOSC commented Nov 2, 2021

I've spent a good hour probing docs(and sample notebooks) between openscm-units, openscm, pint, and pint-pandas. What I have not figured out how to do is make this work:

import pandas as pd
import pint_pandas
from openscm_units import unit_registry
one_co2 = unit_registry("CO2")
print(one_co2)

x = pd.DataFrame([[1.0,'Mt CO2']], columns=['Value', 'Unit'])
x.astype({'Value': 'pint[Mt CO2]'}).dtypes

My one_co2 is 1 CO2, but pint complains: UndefinedUnitError: 'CO2' is not defined in the unit registry

What am I missing? What changes could I help put into the docs for others to use openscm-units successfully?

@mikapfl
Copy link
Member

mikapfl commented Nov 2, 2021

Hi,

could you post a minimum reproducing example of what you are trying to do? Running the code you posted, I get:

NameError                                 Traceback (most recent call last)
<ipython-input-1-c1071259d82c> in <module>
      2 one_co2 = unit_registry("CO2")
      3 
----> 4 x = shell_df[shell_df['Unit']=='Mt CO2']
      5 x.astype({'Value': 'pint[Mt CO2]'}).dtypes

NameError: name 'shell_df' is not defined

And I'm not completely sure what you are trying to do.

Cheers,

Mika

@MichaelTiemannOSC
Copy link
Author

Fixed code so you can run it. Sorry!

@mikapfl
Copy link
Member

mikapfl commented Nov 2, 2021

Hi,

I haven't worked much with pint-pandas (I did more using pint-xarray because we decided for xarray instead of pandas), but from what I gather the experimental unit support using pint-pandas can be used with custom unit registries like this:

import pandas as pd
import pint_pandas
from openscm_units import unit_registry

# start using the custom unit registry
pint_pandas.PintType.ureg = unit_registry

x = pd.DataFrame({'value': pd.Series([1.0], dtype='pint[Mt CO2]')})

x["value"].pint.to('CH4 kt', 'SARGWP100')

Which will output:

0    47.61904761904762
Name: value, dtype: pint[CH4 * kt]

But note that the integration between pandas and pint is experimental, so probably this code will not work next year anymore because something was changed. Also setting the unit registry on the PintType is not exactly a clean way to do things.

openscm-units itself is a pint-only library, so the difficulty of integrating pint with pandas is not fully in scope for the docs - but probably some chapter in the docs on integrations with other libraries would be useful?

Cheers,

Mika

@znicholls
Copy link
Contributor

Thanks for the responses @mikapfl!

@MichaelTiemannOSC, @mikapfl is right that the pint-pandas integration is a little flimsy at times. You can probably tell that by looking at the number of open issues and pull requests in the pint-pandas repository. The current development path is currently stuck by, essentially, this issue pandas-dev/pandas#35131. If you want to dig deeper, see also pandas-dev/pandas#43196 (essentially duplicate of first issue), pandas-dev/pandas#43195 (same as previous but with slightly more info), pandas-dev/pandas#35127 https://github.com/pandas-dev/pandas/pull/39790/files pandas-dev/pandas#39830 (PRs attempting to implement fixes which all stalled as this seems to not be anyone's priority unfortunately).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants