To check for NaN (Not a Number) values in Python, use the following methods depending on your use case and libraries:
1. Using math.isnan()
(Vanilla Python)
Checks if a float is NaN.
Works only for float-type NaN (raises TypeError
for non-float inputs).
import math
x = float('nan')
print(math.isnan(x)) # True
# Handle non-float inputs safely:
y = "not a number"
try:
print(math.isnan(y)) # Raises TypeError
except TypeError:
print(f"{y} is not a float")
2. Using numpy.isnan()
(NumPy)
Checks for NaN in NumPy arrays or scalars.
Fails for non-float/array inputs.
import numpy as np
# For scalars
x = np.nan
print(np.isnan(x)) # True
# For arrays
arr = np.array([1.0, np.nan, 3.0])
print(np.isnan(arr)) # [False True False]
3. Using pandas.isna()
(Pandas)
Best for DataFrames/Series and handles all types (NaN, None, NaT).
import pandas as pd
# For scalars
print(pd.isna(np.nan)) # True
print(pd.isna(None)) # True
print(pd.isna('text')) # False
# For DataFrames/Series
df = pd.DataFrame({'A': [1, np.nan, None]})
print(df['A'].isna())
# 0 False
# 1 True
# 2 True
4. Check via Self-Comparison (Pure Python)
NaN is not equal to itself, so use:
x = float('nan')
print(x != x) # True (only for NaN)
y = 5
print(y != y) # False
5. Check NaN in Lists/Collections
Combine with list comprehensions or map()
:
import math
data = [1.0, float('nan'), 'text', None]
# Method 1: Using math.isnan (only floats)
nan_floats = [item for item in data if isinstance(item, float) and math.isnan(item)]
print(nan_floats) # [nan]
# Method 2: Using pandas.isna (handles all types)
import pandas as pd
nan_values = [item for item in data if pd.isna(item)]
print(nan_values) # [nan, None]
Key Differences
Method | Scope | Handles Non-Floats? | Works with Libraries |
---|---|---|---|
math.isnan() | Pure Python | ❌ (floats only) | – |
numpy.isnan() | NumPy | ❌ (arrays/scalars) | NumPy |
pandas.isna() | Pandas | ✔️ (None, NaT, etc.) | Pandas |
Self-comparison | Pure Python | ✔️ (floats only) | – |
When to Use Which
- Pandas/DataFrames: Use
pd.isna()
. - NumPy Arrays: Use
np.isnan()
. - Pure Python (Float NaN): Use
math.isnan()
orx != x
. - Mixed Data Types: Use
pd.isna()
with a try-except block.
Example: Clean a List of Mixed Data
import pandas as pd
data = [1, None, np.nan, 'text', float('nan')]
cleaned = [item for item in data if not pd.isna(item) or isinstance(item, str)]
print(cleaned) # [1, 'text']