First replace() infs with NaN:

df.replace([np.inf, -np.inf], np.nan, inplace=True)

and then drop NaNs via dropna():

df.dropna(subset=["col1", "col2"], how="all", inplace=True)

For example:

>>> df = pd.DataFrame({"col1": [1, np.inf, -np.inf], "col2": [2, 3, np.nan]})
>>> df
   col1  col2
0   1.0   2.0
1   inf   3.0
2  -inf   NaN

>>> df.replace([np.inf, -np.inf], np.nan, inplace=True)
>>> df
   col1  col2
0   1.0   2.0
1   NaN   3.0
2   NaN   NaN

>>> df.dropna(subset=["col1", "col2"], how="all", inplace=True)
>>> df
   col1  col2
0   1.0   2.0
1   NaN   3.0

The same method also works for Series.

Answer from Andy Hayden on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.isna.html
pandas.DataFrame.isna — pandas 3.0.1 documentation
Detect missing values · Return a boolean same-sized object indicating if the values are NA. NA values, such as None or numpy.NaN, gets mapped to True values. Everything else gets mapped to False values. Characters such as empty strings '' or numpy.inf are not considered NA values
🌐
GeeksforGeeks
geeksforgeeks.org › python › check-if-dataframe-contains-infinity-in-python-pandas
Check if dataframe contains infinity in Python - Pandas - GeeksforGeeks
July 23, 2025 - Explanation: np.isinf(df.values) checks if any value in the DataFrame is infinite and returns a boolean array. .any() checks if there is at least one True value indicating infinity.
🌐
TutorialsPoint
tutorialspoint.com › article › python-check-if-pandas-dataframe-contains-infinity
Python - Check if Pandas dataframe contains infinity
To check, use the isinf() method. To find the count of infinite values, use sum(). At first, let us import the required libraries with their respective aliases − · import pandas as pd import numpy as np · Create a dictionary of list.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to check if a pandas dataframe contains infinity
5 Best Ways to Check if a Pandas DataFrame Contains Infinity - Be on the Right Side of Change
March 4, 2024 - This code snippet creates a pandas DataFrame and utilizes the NumPy function isinf() to derive a boolean DataFrame that indicates the presence of infinite values.
🌐
TutorialsPoint
tutorialspoint.com › python-pandas-check-and-display-row-index-with-infinity
Python Pandas – Check and Display row index with infinity
To check and display row index, use the isinf() with any(). At first, let us import the required libraries with their respective aliases − import pandas as pd import n
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.isin.html
pandas.DataFrame.isin — pandas 3.0.1 documentation
Whether each element in the DataFrame is contained in values · The result will only be true at a location if all the labels match. If values is a Series, that’s the index. If values is a dict, the keys must be the column names, which must match. If values is a DataFrame, then both the index ...
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.isna.html
pandas.Series.isna — pandas 2.3.3 documentation
September 4, 2024 - Return a boolean same-sized object indicating if the values are NA. NA values, such as None or numpy.NaN, gets mapped to True values. Everything else gets mapped to False values. Characters such as empty strings '' or numpy.inf are not considered NA values (unless you set pandas.options.mo...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.4 Manual
>>> import numpy as np >>> np.isinf(np.inf) True >>> np.isinf(np.nan) False >>> np.isinf(-np.inf) True >>> np.isinf([np.inf, -np.inf, 1.0, np.nan]) array([ True, True, False, False])
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.2 Manual
numpy.isinf(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isinf'>#
🌐
W3Schools
w3schools.com › python › ref_math_isinf.asp
Python math.isinf() Method
# Import math Library import math # Check whether the values are infinite or not print(math.isinf(56)) print(math.isinf(-45.34)) print(math.isinf(+45.34)) print(math.isinf(math.inf)) print(math.isinf(float("nan"))) print(math.isinf(float("inf"))) print(math.isinf(float("-inf"))) print(math.isinf(-math.inf)) Try it Yourself »
🌐
w3resource
w3resource.com › python-exercises › pandas › python-pandas-data-frame-exercise-52.php
Pandas: Remove infinite values from a given DataFrame - w3resource
Write a Pandas program to replace all infinite values in a DataFrame with NaN and then fill them with the column median.
🌐
Educative
educative.io › answers › what-is-numpyisinf-in-python
What is numpy.isinf() in Python?
Python’s numpy.isinf() is used to test if an element is a positive/negative infinity or not.
Top answer
1 of 4
2

All you have to do is wrap pd.isnull in a way that in case it gets an iterable it will be forced to check it element-wise. This way you will always get a scalar boolean as output.

from collections import Iterable

def is_scalar_null(value):
    if isinstance(value, Iterable):
        return all(not pd.isnull(v) for v in value)
    return not pd.isnull(value)

assert is_scalar_null(3)
assert is_scalar_null([1, 2])
assert is_scalar_null(pd.Series([1]))
assert not is_scalar_null(None)
assert not is_scalar_null(np.nan)
assert not is_scalar_null([np.nan, 1])
assert not is_scalar_null(pd.Series([np.nan, 1]))

You can then patch the actual pd.isnull, but I can not say that I suggest doing so.

from collections import Iterable

orig_pd_is_null = pd.isnull

def is_scalar_null(value):
    if isinstance(value, Iterable):
        return all(not orig_pd_is_null(v) for v in value)
    return not orig_pd_is_null(value)

pd.isnull = is_scalar_null

assert pd.isnull(3)
assert pd.isnull([1, 2])
assert pd.isnull(pd.Series([1]))
assert not pd.isnull(None)
assert not pd.isnull(np.nan)
assert not pd.isnull([np.nan, 1])
assert not pd.isnull(pd.Series([np.nan, 1]))

This approach will probably break in case of nested iterables, but that can be fixed by using recursion in is_scalar_null.

2 of 4
0

This is an extension to @DeepSpace's solution. For NumPy arrays and, by extension, numeric Pandas series, you can utilize numba for JIT-compiling your loop. all / any with a generator comprehension is generally less efficient, and often prohibitively expensive when your NaN value is near the end of your array.

For example, in an extreme case we see a ~240x performance differential:

from collections import Iterable
from numba import njit

def any_null(arr):
    for i in range(len(arr)):
        if np.isnan(arr[i]):
            return True
    return False

def is_scalar_null(value, jit_flag=True):
    checker = njit(any_null) if jit_flag else any_null
    if isinstance(value, pd.Series):
        return checker(value.values)
    elif isinstance(value, np.ndarray):
        return checker(value)
    elif isinstance(value, Iterable):
        return all(not pd.isnull(v) for v in value)
    return not pd.isnull(value)

np.random.seed(0)
A = np.random.random(10**7)
A[-1] = np.nan

%timeit is_scalar_null(A, jit_flag=True)  # 74.3 ms per loop
%timeit is_scalar_null(A, jit_flag=False) # 17.6 s per loop
🌐
GeeksforGeeks
geeksforgeeks.org › check-if-the-value-is-infinity-or-nan-in-python
Check if the value is infinity or NaN in Python - GeeksforGeeks
March 24, 2023 - import pandas as pd x = float("nan") ... != nan · To check for infinite in python the function used is math.isinf() which only checks for infinite....
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v1.13 Manual
June 26, 2025 - numpy.isinf(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'isinf'>¶