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

Assigning a NaN to Quantity does not work #127

Closed
hgrecco opened this issue Apr 4, 2014 · 5 comments
Closed

Assigning a NaN to Quantity does not work #127

hgrecco opened this issue Apr 4, 2014 · 5 comments

Comments

@hgrecco
Copy link
Owner

hgrecco commented Apr 4, 2014

Assigning a NaN to Quantity does not work:

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> q = [1, 2, 3, 4] * ureg.meter
>>> q[0] = np.nan
Traceback (most recent call last):
...
ValueError
>>> q[0] = float('NaN')
Traceback (most recent call last):
...
ValueError

See #126

@mankoff
Copy link
Contributor

mankoff commented Apr 4, 2014

The behavior here is the same as the python-quantities package, and does work in my opinion.

q[0] = np.nan * ureg.meter

is more cumbersome, but is also logical (to me).

@hgrecco
Copy link
Owner Author

hgrecco commented Apr 4, 2014

It is logical, but given that NaN is Not a Number, i would be fine if this works:

q[0] = np.nan

@mankoff
Copy link
Contributor

mankoff commented Apr 5, 2014

Agreed that should work. But the edge case of Inf should not work like that, since infinity does have units.

@hgrecco
Copy link
Owner Author

hgrecco commented Apr 7, 2014

agreed

@s-celles
Copy link

It should be

q = [1.0, 2, 3, 4] * ureg.meter
q[0] = np.nan

because

In [32]: q = [1, 2, 3, 4] * ureg.meter
    ...: q[0] = np.nan
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-b94d2d97deac> in <module>
      1 q = [1, 2, 3, 4] * ureg.meter
----> 2 q[0] = np.nan

~/anaconda3/lib/python3.7/site-packages/pint/quantity.py in __setitem__(self, key, value)
   1407         try:
   1408             if math.isnan(value):
-> 1409                 self._magnitude[key] = value
   1410                 return
   1411         except (TypeError, DimensionalityError):

ValueError: cannot convert float NaN to integer

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

No branches or pull requests

3 participants