Net-informations.com

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?

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.

Try to convert as integer:

This will generate ValueError: Cannot convert non-finite values (NA or inf) to integer

So, use Nullable Integer Data Types (e.g. 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.

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() .

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

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) )










net-informations.com (C) 2022    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About