Call df.isna().any().any() (or bool(df.isna().values.any())) to get a single fast True/False boolean indicating whether at least one NaN, None, or pd.NA exists anywhere in the DataFrame—or inspect df.isna().sum() to see exact missing-value counts per column.
Basic Example
import numpy as np
import pandas as pd
df = pd.DataFrame({
"name": ["Alice", "Bob", None],
"age": [28, np.nan, 35],
"score": [90, 85, 88],
})
# 1. Recommended: Single boolean check across the entire DataFrame
has_any_nan = bool(df.isna().any().any())
print(has_any_nan) # Output: True
# 2. Check which columns contain at least one NaN
print(df.isna().any())
# Output:
# name True
# age True
# score False
# dtype: bool
# 3. Count NaNs per column and total NaNs across the whole DataFrame
print(df.isna().sum()) # Per-column counts: name=1, age=1, score=0
print(df.isna().sum().sum()) # Total missing cells: 2
# 4. Filter and inspect only the rows containing at least one NaN
rows_with_nan = df[df.isna().any(axis=1)]
Key Details & Edge Cases
df.isna()vs.df.isnull():df.isna()anddf.isnull()are exact aliases in Pandas—they produce identical results across every dtype (float,object,string,Int64,datetime64). PEP 8 and modern Pandas conventions favorisna()(paired withnotna()anddropna()).
- What counts as missing (
NaN,None,pd.NA,pd.NaT) vs. what does NOT:isna()detectsnp.nan, PythonNone,pd.NA(nullable integer/boolean/string missing marker), andpd.NaT(missing datetime/timedelta).- It does not treat empty strings (
""), whitespace (" "), string literals ("NaN","null"), ornp.inf/-np.infas missing—usedf.replace(["", "NaN", "null"], np.nan)orpd.option_context("mode.use_inf_as_na", True)(ornp.isinf()) if those should count as missing.
- Return type (
numpy.bool_vs. Pythonbool):df.isna().any().any()returns a NumPy scalar (np.True_/np.False_). It works fine inifstatements (if df.isna().any().any():), but wrap it inbool(...)if you serialize it to JSON or runassert result is True.
- Performance on large DataFrames (
df.isna().values.any()vs.df.isna().any().any()):- On homogeneous numeric DataFrames,
df.isna().values.any()avoids creating an intermediate per-columnSeriesand short-circuits quickly in NumPy. On mixed-dtype DataFrames (combiningfloat64,object, anddatetime64),df.isna().any().any()avoids forcing a full-table NumPy array copy (values) and uses less memory.
- On homogeneous numeric DataFrames,
- Checking a single column (
Series): For one column, calldf["age"].isna().any()(only one.any()is needed since aSeriesis 1D).
Comparison of Pandas Missing-Value Detection Methods
| Method | Syntax | Return Type | Short-Circuits Early? | Best Used When |
|---|---|---|---|---|
df.isna().any().any() |
bool(df.isna().any().any()) |
bool (True/False) |
Yes (per-column reduction) | Fast boolean guard check across mixed-dtype DataFrames. |
df.isna().values.any() |
bool(df.isna().values.any()) |
bool (True/False) |
Yes (NumPy C-level scan) | Fast boolean check on large, uniform numeric DataFrames. |
df.isna().any() |
df.isna().any() |
Series[bool] (indexed by col) |
Yes | Identifying which specific columns contain missing data. |
df.isna().sum() |
df.isna().sum() |
Series[int] (indexed by col) |
No (scans all cells) | Auditing exact missing-value counts per column during EDA. |
df[df.isna().any(axis=1)] |
df[df.isna().any(axis=1)] |
Filtered DataFrame |
No | Extracting and inspecting the exact rows that have missing values. |
You must log in or register to comment.
