Use math.isnan() for standard Python floats, np.isnan() for NumPy arrays, and .isna() (or .isnull()) for Pandas Series and DataFrames. Because NaN (Not a Number) is never equal to itself under IEEE 754 floating-point rules (float('nan') == float('nan') evaluates to False), you must use these dedicated functions rather than == or is.

1. Standard Python (math.isnan & cmath.isnan)

For built-in Python scalar numbers, import the math module (or cmath for complex numbers).

import math

val = float("nan")

# Standard equality checks fail for NaN
print(val == float("nan"))  # Output: False

# Use math.isnan() for reliable scalar detection
print(math.isnan(val))      # Output: True
print(math.isnan(42.0))     # Output: False

Note: math.isnan() raises a TypeError if passed None or a non-numeric string. Guard mixed-type variables with isinstance(x, float) and math.isnan(x).

2. NumPy (np.isnan)

For NumPy arrays and scalars, np.isnan() performs fast, vectorized element-wise checks and returns a boolean array of the same shape.

import numpy as np

arr = np.array([1.0, np.nan, 3.5, np.nan])

# Element-wise boolean mask
mask = np.isnan(arr)
print(mask)               # Output: [False  True False  True]

# Check if ANY or ALL elements are NaN
print(np.isnan(arr).any())  # Output: True
print(np.isnan(arr).sum())  # Output: 2

3. Pandas (isna / isnull)

Pandas uses .isna() (and its identical alias .isnull()) to detect np.nan, None, pd.NA, and pd.NaT across mixed data types—including strings, objects, and datetimes.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    "age": [25, np.nan, 30],
    "name": ["Ali", None, "Sara"]
})

# Element-wise boolean DataFrame
print(df.isna())

# Count missing values per column
print(df.isna().sum())
# Output:
# age     1
# name    1

# Check if the entire DataFrame has any missing values
print(df.isna().values.any())  # Output: True

Quick Comparison Across Libraries

Library Function / Method Supported Types Handles None?
Standard Python math.isnan(x) int, float scalars No (raises TypeError)
NumPy np.isnan(arr) Numeric ndarray & scalars No (raises TypeError on object arrays)
Pandas df.isna() / pd.isna(x) Scalars, Series, DataFrame (any dtype) Yes (treats None, np.nan, pd.NA, and pd.NaT as missing)

Frequently Asked Questions

Why does x == float('nan') return False in Python?
Python follows the IEEE 754 floating-point standard, which specifies that NaN represents an undefined or unrepresentable value and therefore cannot equal anything—not even another NaN.

Is there any difference between .isna() and .isnull() in Pandas?
No. Both methods execute the exact same underlying code in Pandas; .isna() is generally preferred in modern codebases for consistency with .dropna() and .fillna().