Pandas: Valueerror: Cannot Convert Float Nan to Integer

Pandas: ValueError: cannot convert float NaN to integer

For identifying NaN values use boolean indexing:

print(df[df['x'].isnull()])

Then for removing all non-numeric values use to_numeric with parameter errors='coerce' - to replace non-numeric values to NaNs:

df['x'] = pd.to_numeric(df['x'], errors='coerce')

And for remove all rows with NaNs in column x use dropna:

df = df.dropna(subset=['x'])

Last convert values to ints:

df['x'] = df['x'].astype(int)

Numpy: ValueError: cannot convert float NaN to integer (Python)

Designate a type for your array of float32 (or float16, float64, etc. as appropriate)

import numpy as np

A = np.array([10, 20, 30, 40, 50, 60, 70], dtype=np.float32)
C=[2,4]

A=np.insert(A,C,np.NaN,axis=0)
print("A =",[A])

A = [array([10., 20., nan, 30., 40., nan, 50., 60., 70.], dtype=float32)]

How can I solve the issue 'ValueError: cannot convert float NaN to integer' in python

You need to say what you want to do with nans. You can either drop those rows (df.dropna()) or replace nans with something else (0 for instance: df.fillna(0))

df["Height (cm)"]= df["Height (cm)"].fillna(0).astype(int)

ValueError: cannot convert float NaN to integer

That is because nan is recognised as a special character for float arrays (a sort of special float), and apparently your x_2 array is int type; and nan cannot be converted to int to fit into your array.

A workaround could be casting your array into floats:

>>>import numpy as np
>>>a = np.array([[1,2,3], [4,5,6]])
>>>a
array([[1, 2, 3],
[4, 5, 6]])

>>>a[0,0] = np.nan
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-8e6dceaece5a> in <module>
----> 1 a[0,0] = np.nan

ValueError: cannot convert float NaN to integer

>>>a = a.astype('float32')
>>>a[0,0] = np.nan
>>>a
array([[nan, 2., 3.],
[ 4., 5., 6.]], dtype=float32)

How to resolve Pandas: ValueError: cannot convert float NaN to integer for a large dataset?

Sample Image

Your commands work for me. Can you try updating your numpy and pandas ?



Related Topics



Leave a reply



Submit