Cannot convert float NaN to integer

NaN is short for Not a Number . It is a numeric data type used to represent any value that is undefined or unpresentable. The ValueError: cannot convert float NaN to integer raised because of Pandas doesn't have the ability to store NaN values for integers.

How to solve cannot convert float NaN to integer?

df['column_name'].astype(np.float).astype("Int32")
From Pandas v0.24, introduces Nullable Integer Data Types which allows integers to coexist with NaNs. This does allow integer NaNs . This is the pandas integer, instead of the numpy integer.
df = pd.DataFrame({'x':[1.0, np.NaN, 3.0, 4.0]}) df
x 0 1.0 1 NaN 2 3.0 3 4.0

Try to convert as integer:

df['x'].astype(int)
This will generate ValueError: Cannot convert non-finite values (NA or inf) to integer So, use Nullable Integer Data Types (e.g. Int64).
df['x'].astype('Int64') 0 1 1 <NA> 2 3 3 4 Name: x, dtype: Int64

Solution 2:


how to solve Cannot convert float NaN to integer

Using numpy.nan_to_num()

The numpy.nan_to_num() returns an array or scalar replacing Not a Number ( Not A Number ) with zero, positive_infinity with a very large number and negative_infinity with a very small (or negative) number.
import numpy import math value = numpy.nan value
Out[69]: nan
Here you get the output value is NAN . Next you can check the NAN value using isnan(value) , if it is NAN value then you can convert using nan_to_num() .
if numpy.isnan(value): value = numpy.nan_to_num(value) value
Out[70]: 0.0
Here you can see the nan_to_num() changed the NaN value to 0.0 which can then be converted into an integer. Full Source
import numpy import math value = numpy.nan value if numpy.isnan(value): value = numpy.nan_to_num(value) value
If you are not satisfied with the above solutions, 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) )