Skip to content
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

parse_units instead of parse_expression #40

Closed
TomNicholas opened this issue Oct 31, 2020 · 0 comments · Fixed by #41
Closed

parse_units instead of parse_expression #40

TomNicholas opened this issue Oct 31, 2020 · 0 comments · Fixed by #41

Comments

@TomNicholas
Copy link
Collaborator

Well done on releasing this guys!

I just gave it a trial by fire on my actual data, and found a bug. My data already has units stored in the attributes, written like 'm^2' and so on.

But the quantify method fails with ValueError: Integers to negative integer powers are not allowed when I try to convert something like m^-1. Turns out this is pint, not pint-xarray:

In [30]: ureg.parse_expression('m')                                                                                                                                                           
Out[30]: array(1) <Unit('meter')>

In [31]: ureg.parse_expression('m')                                                                                                                                                           
Out[31]: array(1) <Unit('meter')>

In [32]: ureg.parse_expression('m^1')                                                                                                                                                         
Out[32]: array(1) <Unit('meter')>

In [33]: ureg.parse_expression('m^0.5')                                                                                                                                                       
Out[33]: array(1.) <Unit('meter ** 0.5')>

In [34]: ureg.parse_expression('m^-0.5')                                                                                                                                                      
Out[34]: array(1.) <Unit('1 / meter ** 0.5')>

In [35]: ureg.parse_expression('m^-1')
ValueError: Integers to negative integer powers are not allowed.

@jthielen maybe you know a good reason why it does this? I can't see one...

But it seems it can be solved just by using parse_units instead of parse_expression in _decide_units. I think that's the more appropriate choice of method to call anyway - if we're parsing the str attribute of a DataArray we expect to be parsing a unit, not a mathematical expression.

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

Successfully merging a pull request may close this issue.

2 participants