hgrecco / pint-pandas

Pandas support for pint
Other
172 stars 42 forks source link

TypeError: float() argument must be a string or a number, not 'NAType' #167

Closed MichaelTiemannOSC closed 1 year ago

MichaelTiemannOSC commented 1 year ago

Related to #26 (?):

import pandas as pd
import pint_pandas

ser = pd.Series([1.0, pd.NA, 3.0]).astype('Float64')
print(ser)
ser_f = ser.astype('float64')
print(ser_f)
ser_fm = ser_f.astype('pint[m]')
print(ser_fm)
ser_m = ser.astype('pint[m]')
print(ser_m)

Output:

(pint-vanilla) iMac-Pro:test michael$ python pr133.py 
0     1.0
1    <NA>
2     3.0
dtype: Float64
0    1.0
1    NaN
2    3.0
dtype: float64
0    1.0
1    nan
2    3.0
dtype: pint[meter]
/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pint_pandas/pint_array.py:227: RuntimeWarning: pint-pandas does not support magnitudes of object. Converting magnitudes to float.
  warnings.warn(
Traceback (most recent call last):
  File "/Users/michael/Documents/GitHub/MichaelTiemannOSC/ITR/test/pr133.py", line 8, in <module>
    ser_m = ser.astype('pint[m]')
  File "/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pandas/core/generic.py", line 6240, in astype
    new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
  File "/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pandas/core/internals/managers.py", line 448, in astype
    return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
  File "/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pandas/core/internals/managers.py", line 352, in apply
    applied = getattr(b, f)(**kwargs)
  File "/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pandas/core/internals/blocks.py", line 526, in astype
    new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
  File "/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pandas/core/dtypes/astype.py", line 299, in astype_array_safe
    new_values = astype_array(values, dtype, copy=copy)
  File "/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pandas/core/dtypes/astype.py", line 227, in astype_array
    values = values.astype(dtype, copy=copy)
  File "/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pandas/core/arrays/masked.py", line 456, in astype
    return eacls._from_sequence(self, dtype=dtype, copy=copy)
  File "/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pint_pandas/pint_array.py", line 500, in _from_sequence
    return cls(scalars, dtype=dtype, copy=copy)
  File "/Users/michael/opt/miniconda3/envs/pint-vanilla/lib/python3.9/site-packages/pint_pandas/pint_array.py", line 231, in __init__
    values = values.astype(float)
TypeError: float() argument must be a string or a number, not 'NAType'

I don't see why I should need to crosswalk to float64 before I can cast to meters (or whatever).

MichaelTiemannOSC commented 1 year ago

Fixed in Pint-Pandas 0.4