Pandas 0.13.1 Python 3 ValueError Unable to convert Nan to integer

I recently switched to python 3 from 2.7, reinstalled all my libraries and fixed the scripts manually without py2to3. The part of my script that sorts the odds and even getsValueError: Cannot convert Nan to integer

import pandas as pd

def ExtractU(df):
   is_even = df['IDs'].str.extract('(\d+).*').astype(int) % 2 == 0
   Even=df[is_even]
   Odd=df[~is_even]
   return Odd

So, to verify this, I changed it to print the framework using:

print (df['IDs'].str.extract('(\d+).*'))

and got an array NanSo, I assume that the function pandasmay be deprecated because the regex does not work or is it because the version has been downgraded from one of the development versions that I previously used.

How do I return returned numeric values?

So, I redid the code in a smaller set, and it works that something is wrong with a large set of data.

import pandas as pd

df=pd.DataFrame({'ID': ['10A','6.5', '4 A', '3 1/2'], 'Name': ['J','K','L','M']})


def ExtractU(df):
    is_even = df['ID'].str.extract('(\d+).*').astype(int) % 2 == 0
    Even=df[is_even]
    Odd=df[~is_even]
    return Even

print (ExtractU(df))

, df=df.drop_duplicates(['Name','ID']).set_index('Name'), , :

           ID
Name        
Gary          6445
Jerry         6239
Anza          3828
Kang          3745
[28 rows x 1 columns]

, . , Nan . is_even = df['ID'].str.extract('(\d+).*').astype(int) % 2 == 0, ValueError.

:

Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
  File "C:\Python33\lib\site-packages\pandas\core\generic.py", line 2018, in astype
    dtype, copy=copy, raise_on_error=raise_on_error)
  File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 2416, in astype
    return self.apply('astype', *args, **kwargs)
  File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 2375, in apply
    applied = getattr(blk, f)(*args, **kwargs)
  File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 427, in astype
    values=values)
  File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 444, in _astype
    values = com._astype_nansafe(self.values, dtype, copy=True)
  File "C:\Python33\lib\site-packages\pandas\core\common.py", line 2222, in _astype_nansafe
    return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
  File "lib.pyx", line 733, in pandas.lib.astype_intsafe (pandas\lib.c:12697)
  File "util.pxd", line 59, in util.set_value_at (pandas\lib.c:49357)
ValueError: cannot convert float NaN to integer

bool

0
1

is_even = df['IDs'].str.extract('(\d+).*').astype(int) % 2 == 0

is_even = df['IDs'].str.extract('(\d+).*').astype(float) % 2 == 0

NaNs float, int. ,

In [150]: ser = pd.Series(['1','2','three'])

In [151]: is_even = ser.str.extract('(\d+).*').astype('float') % 2 == 0

In [152]: ser[~is_even]
Out[152]: 
0        1
2    three
dtype: object

, , NaN, 'three', "".


, Even=df[is_even] , .

+1

All Articles