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:

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) )
Related Topics
- ImportError: No module named pandas
- What is SettingWithCopyWarning?
- UnicodeDecodeError while reading CSV file
- How to fix CParserError: Error tokenizing data
- ValueError: cannot reindex from a duplicate axis
- How to fix "Unnamed: 0" column in a pandas DataFrame
- ValueError: Unknown label type: 'unknown'
- ValueError: Length of values does not match length of index
- ValueError: The truth value of an array with more than..