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

We Are Going To Discuss About ValueError: Cannot convert non-finite values (NA or inf) to integer. So lets Start this Python Article.

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

  1. How to solve ValueError: Cannot convert non-finite values (NA or inf) to integer

    Assuming that the budget does not contain infinite values, the problem may be because you have nan values. These values are usually allowed in floats but not in ints.
    You can:
    Drop na values before converting
    Or, if you still want the na values and have a recent version of pandas, you can convert to an int type that accepts nan values (note the i is capital):

    df['budget'] = df['budget'].astype("Int64")

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

    Assuming that the budget does not contain infinite values, the problem may be because you have nan values. These values are usually allowed in floats but not in ints.
    You can:
    Drop na values before converting
    Or, if you still want the na values and have a recent version of pandas, you can convert to an int type that accepts nan values (note the i is capital):

    df['budget'] = df['budget'].astype("Int64")

Solution 1

Assuming that the budget does not contain infinite values, the problem may be because you have nan values. These values are usually allowed in floats but not in ints.

You can:

  1. Drop na values before converting
  2. Or, if you still want the na values and have a recent version of pandas, you can convert to an int type that accepts nan values (note the i is capital):

df['budget'] = df['budget'].astype("Int64")

Original Author Mohammad Of This Content

Solution 2

Try this notice the capital “i” in Int64

df['budget'] = df['budget'].astype("Int64") 

you might have some NaN values in this column which might be the reason for this issue

From pandas docs:

Changed in version 1.0.0: Now uses pandas.NA as the missing value rather than numpy.nan

Follow the link to find out more:

https://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html

Or you could fill the NaN/NA values with 0 and than do .astype("int64")

df['budget'] = df['budget'].fillna(0) 

Original Author Tomer Poliakov Of This Content

Solution 3

Check for any null values present in the column.
If there are no null values. Try using apply() instead of astype()

df['budget'] = df['budget'].apply("int64")

Original Author Pulkit Chandel Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment