Check if dataframe contains infinity in Python – Pandas



When working with data in Python using Pandas, it is important to ensure that the data is clean and contains no unexpected values that could lead to errors in analysis or modeling. One such unexpected value is infinity. In this article, we will explore how to check if a Pandas dataframe contains infinity values.

Method 1: Using NumPy

The NumPy library provides a convenient way to check if a dataframe contains infinity values. We can use the isinf() function from NumPy to check for infinity values in the dataframe. Here is an example:


import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [1, 2, np.inf], 'B': [4, np.inf, 6]})

if np.isinf(df.values).any():
print("Dataframe contains infinity values.")
else:
print("Dataframe does not contain infinity values.")

In this example, we first create a Pandas dataframe with some values, including infinity values. We then use the isinf() function from NumPy to check for infinity values in the dataframe. The any() function is used to check if any of the values are infinity. If any of the values are infinity, the message “Dataframe contains infinity values.” is printed. Otherwise, the message “Dataframe does not contain infinity values.” is printed.

Method 2: Using Pandas

Pandas also provides a way to check if a dataframe contains infinity values using the isin() method. Here is an example:



import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [1, 2, np.inf], 'B': [4, np.inf, 6]})

if df.isin([np.inf, -np.inf]).any().any():
print("Dataframe contains infinity values.")
else:
print("Dataframe does not contain infinity values.")

In this example, we again create a Pandas dataframe with some values, including infinity values. We then use the isin() method to check for infinity values in the dataframe. The any() function is used twice to check if any of the values are infinity or negative infinity. If any of the values are infinity or negative infinity, the message “Dataframe contains infinity values.” is printed. Otherwise, the message “Dataframe does not contain infinity values.” is printed.

Conclusive Example code –

import numpy as np
import pandas as pd

# Creating a sample DataFrame
df = pd.DataFrame({
   'col1': [1.0, 2.0, np.inf, 4.0],
   'col2': [5.0, np.inf, 7.0, 8.0]
})

# Method 1: Using isin() method with a list of infinity values
inf_values = [np.inf, -np.inf]
has_inf = df.isin(inf_values).any().any()
print("Method 1: Does the DataFrame have infinity? ", has_inf)

# Method 2: Using numpy's isinf() method
has_inf = np.isinf(df.values).any()
print("Method 2: Does the DataFrame have infinity? ", has_inf)

# Method 3: Using numpy's isinf() method with DataFrame's replace() method
df = df.replace([np.inf, -np.inf], np.nan)
has_inf = df.isnull().values.any()
print("Method 3: Does the DataFrame have infinity? ", has_inf)

 

These code examples demonstrate three different methods for checking whether a Pandas DataFrame contains infinity. The first method uses the isin() method with a list of infinity values, the second method uses numpy’s isinf() method, and the third method uses numpy’s isinf() method with the DataFrame’s replace() method to convert infinity values to NaN. All three methods provide a simple and efficient way to check for infinity in a DataFrame.

Last Updated on May 11, 2023 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs