Check for NaN values in Python
A Step-by-Step Guide to Checking for NaN values in Python
Published by Carlo van Wyk on June 15, 2025 in Python

Checking for NaN (Not a Number) values in Python can be done through several methods, depending on your specific needs and the libraries you're using.
Different ways to check for NaN in Python
Using the math module:
import math
# Check if a value is NaN
x = float('nan')
is_nan = math.isnan(x)
Using NumPy:
import numpy as np
# Single value check
x = np.nan
is_nan = np.isnan(x)
# Array check
array = np.array([1, np.nan, 3, np.nan])
nan_mask = np.isnan(array)
Using Pandas:
import pandas as pd
# Check for NaN in a Series
series = pd.Series([1, np.nan, 3, None])
nan_check = series.isna()
# Check for NaN in a DataFrame
df = pd.DataFrame({'A': [1, np.nan, 3], 'B': [np.nan, 5, 6]})
nan_mask = df.isna()
# Count NaN values
nan_count = df.isna().sum()
Key differences to note:
math.isnan()
works with single float values onlynumpy.isnan()
handles arrays and individual valuespandas.isna()
detects both NaN and None values in Series/DataFrames
For most data analysis tasks, the Pandas
method is recommended as it handles different types of missing values consistently.