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

In this post, we are going to understand how to fix the ValueError: Cannot convert non-finite values (NA or inf) to an integer. The error gets raised in Python when we try to convert Pandas dataframe column of float type that contains NAN into an integer type.This error can be solve using pandas data frame method fillna(),dropna() and replace() .

What is ValueError: cannot convert float NaN to integer


In the below example the data frame “Mark” column contains nan and float values. Whenever we are converting datatype of marks columns to int. The error “ValueError: cannot convert float NaN to integer ” is encountered or “ValueError: Cannot convert non-finite values (NA or inf) to integer”.

import pandas as pd
import numpy as np
    
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[np.nan,98.9, 99.90,100.0],
    'Subject': ['Math', 'Math', 'Math', np.inf]
}
 
dfobj = pd.DataFrame(data)
 
dfobj['Marks'] =  dfobj['Marks'].astype(int)
print(dfobj )

Output

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

1. Fix Cannot convert non-finite values (NA or inf) to integer using fillna()


To solve this error, we can replace all the nan values in the “Marks” column with zero or a value of your choice like fillna(100) by using the fillna(0) method pf the pandas data frame. In the next step, Now the type of ‘Marks’ column can be converted to ‘integer’ without any error.

  • Import Pandas module using “import pandas as pd”
  • Import Pandas module using “import numpy as np”
  • To replace nan value zero call dfobj.fillna(0) method
  • The final step replace the ‘Mark’ column type with float to integer
import pandas as pd
import numpy as np
    
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[np.nan,98.9, 99.90,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}
 
dfobj = pd.DataFrame(data)

dfobj = dfobj.fillna(0)
 
dfobj['Marks'] = dfobj['Marks'].astype(int)
print(dfobj )
print('\n type of Mark column is : ',dfobj['Marks'].dtype )

Output

    Name  Marks Subject
0   Jack      0    Math
1   Rack     98    Math
2    Max     99    Math
3  David    100     Phy

type of Mark column is :int32

2. How to fix ValueError: cannot convert float NaN to integer using dropna()


In this Python example we will learn How to fix ValueError: cannot convert float NaN to integer using dropna(). First of all, we will drop all the NAN values from the “Marks” column using the Pandas data frame dropna() method and after that type of ‘Marks’ column can be converted to ‘integer’ without any error.

  • import Pandas module using “import pandas as pd”
  • Import Pandas module using “import numpy as np”
  • To replace nan value zero call dfobj.dropna() method to drop all nan values in Marks column.
  • The final step replace the ‘Mark’ column type with float to integer.
import pandas as pd
import numpy as np
    
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[np.nan,98.9, 99.90,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}
 
dfobj = pd.DataFrame(data)

dfobj = dfobj.dropna()
 
dfobj['Marks'] = dfobj['Marks'].astype(int)

print(dfobj )
print('\n type of Mark column:',dfobj['Marks'].dtype )



Output

    Name  Marks Subject
1   Rack     98    Math
2    Max     99    Math
3  David    100     Phy

3. How to fix ValueError: cannot convert float NaN to integer using replace()


In this Python program, to fix values error, We have used the Pandas data frame replace() method. First, we will replace drop all the NAN values from the “Marks” column into zero and after that convert the type of ‘Marks’ column o ‘integer’ without any error using astype() method.

import pandas as pd
import numpy as np
    
data = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[np.nan,98.9, 99.90,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}
 
dfobj = pd.DataFrame(data)

dfobj['Marks'] = dfobj['Marks'].replace(np.nan, 0)
 
dfobj['Marks'] = dfobj['Marks'].astype(int)

print(dfobj )
print('\ntype of Mark column:',dfobj['Marks'].dtype )


Output

    Name  Marks Subject
0   Jack      0    Math
1   Rack     98    Math
2    Max     99    Math
3  David    100     Phy

 type of Mark column: int32

Summary

In this post we have learned how to fix ValueError: Cannot convert non-finite values (NA or inf) to integer occurs convert Pandas dataframe column type float that contain Nan values to an integer.We can solve it by using data frame methods fillna(),dropna() and replace() .