HOWTO · Python
How to Check for NaN Values in Python
Learn how to check for NaN values in Python scalars, lists, NumPy arrays, and Pandas data, and how to count values Pandas recognizes as missing.
On this page
NaN means “Not a Number” and is a special floating-point value. Use math.isnan() for a scalar float or numeric-float list, numpy.isnan() for NumPy arrays, and pandas.isna() for Pandas data. These checks report NaN or library-defined missing values; they do not test a database for SQL NULL.
Check Numbers and Lists with math.isnan()
Python’s math.isnan() returns True for a float NaN and False for an ordinary finite float. For a list of numeric floats, apply it to each item with a list comprehension. This function expects a real number; strings and arbitrary objects are not numeric inputs.
Run this example:
import math
value = 3.5
print(math.isnan(value))
False
For a list, use the same check for each value:
import math
values = [1.0, float("nan"), 4.5]
checks = [math.isnan(value) for value in values]
print(checks)
[False, True, False]
The scalar result is False because 3.5 is not NaN. In the list result, the middle True identifies the NaN. Use sum(checks) to count the True entries because Python treats Boolean values as integers.
Check NumPy Arrays with numpy.isnan()
numpy.isnan() checks array elements element by element and returns a Boolean array with the same shape. In code, the common alias np refers to NumPy, so np.isnan() is the same function as numpy.isnan(). Use np.nan to place a NaN value in an array.
import numpy as np
data = np.array([1.0, np.nan, 4.0])
print(np.isnan(data))
[False True False]
Each True marks an element that is NaN, while each False marks a value that is not NaN. For a multidimensional array, the Boolean result keeps the input shape; use that mask to select matching elements.
Check Pandas Missing Values with pandas.isna()
Pandas isna() returns a Boolean Series or DataFrame matching the input’s index and shape. The module-level spelling pandas.isna() and the corresponding object method, such as df.isna(), locate values Pandas recognizes as missing. isnull() is an equivalent Pandas spelling. Pandas missing-value detection applies to in-memory data; it does not query a database for SQL NULL.
import pandas as pd
df = pd.DataFrame({"A": [1.0, float("nan")], "B": [float("nan"), 2.0]})
missing = pd.isna(df)
print(missing.sum())
A 1
B 1
dtype: int64
Summing the Boolean DataFrame by its default column axis gives the missing-value count for each column. Here, each of columns A and B contains one value Pandas recognizes as missing.
Use value != value as a NaN Check
For an ordinary scalar NaN float, value != value evaluates to True because NaN is unequal to itself. This is a limited alternative, not the general check to use across Python data types: custom objects can define comparison behavior, and array-like values may return element-wise results. Prefer math.isnan(), numpy.isnan(), or pandas.isna() when their input types match your data.