Skip to content

Use-Isna

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Use .isna() or .isnull() instead of == np.nan for detecting missing values.

import numpy as np
df['column'] == np.nan
import numpy as np
df['column'].isna()

Use .isna() or .isnull() for detecting missing values.

A comparison like df['column'] == np.nan doesn’t produce the expected results when checking for missing or NaN (Not a Number) values. This is due to the peculiar nature of NaN: It is not considered equal to any value, even itself.

data = {
'A': [1, 2, np.nan, 4],
'B': [9, 10, 11, 12]
}
df = pd.DataFrame(data)
print(df['A'] == np.nan)

The output:

0 False 1 False 2 False 3 False Name: A, dtype: bool

print(df['A'].isna())

The output:

0 False 1 False 2 True 3 False Name: A, dtype: bool

See also Pandas Docs / Missing Data