Pandas cannot convert non-finite values (na or inf) to integer

Error: pandas cannot convert non-finite values (na or inf) to integer

Pandas is a popular data manipulation library in Python, primarily used for data analysis and cleaning. When working with numerical data, you may encounter a situation where you get the error message “pandas cannot convert non-finite values (na or inf) to integer”. This error typically occurs when you try to convert non-finite values like NaN (Not a Number) or infinity to an integer type.

An example to illustrate this error is when you have a DataFrame with a column containing floating-point numbers, and you want to convert it to integers:

      import pandas as pd

      data = {'number': [1.5, 2.7, NaN, 4.2, float('inf')]}
      df = pd.DataFrame(data)

      # Attempt to convert 'number' column to integer
      df['number'] = df['number'].astype(int)
    

In this example, the DataFrame ‘df’ has a column named ‘number’ that contains floating-point numbers. The line df['number'] = df['number'].astype(int) tries to convert these numbers to integers. However, since the column contains non-finite values like NaN and infinity, the conversion throws an error.

To handle this error, you can replace the non-finite values with a valid number (e.g., 0) or NaN using the fillna() function before converting to an integer:

      # Replace non-finite values with 0
      df['number'] = df['number'].fillna(0).astype(int)
    

With this modification, the non-finite values in the ‘number’ column would be replaced with 0 before converting the column to integer type.

Leave a comment