🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.isnan()

AI & DATA SCIENCE // np-isnan

np.isnan() tests each element of an array and returns a boolean array indicating which elements are NaN (Not a Number).

Syntax

np.isnan(arr)

Deep Dive Course

NaN represents an undefined or indeterminate numeric result, like 0 divided by 0 or the square root of a negative real number, and it has the unusual property of never being equal to anything, including itself — which is exactly why you can't detect it with a plain equality check against np.nan. isnan() instead inspects the actual floating-point bit pattern to correctly identify NaN values, regardless of that equality quirk.

1Understanding np.isnan()

NaN represents an undefined or indeterminate numeric result, like 0 divided by 0 or the square root of a negative real number, and it has the unusual property of never being equal to anything, including itself — which is exactly why you can't detect it with a plain equality check against np.nan. isnan() instead inspects the actual floating-point bit pattern to correctly identify NaN values, regardless of that equality quirk.

💡

Never compare an array directly for equality against np.nan to detect NaN values — since NaN never equals anything, including itself, that comparison always evaluates to False even where NaN is genuinely present; use np.isnan(arr) instead.

editor.html
import numpy as np

arr = np.array([1.0, np.nan, 3.0])
print(np.isnan(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.isnan() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.array([1.0, np.nan, 3.0, np.nan])
print(arr == np.nan)
print(np.isnan(arr))
localhost:3000

3Best Practices

Follow these guidelines when working with np.isnan():

1. Always use np.isnan(arr) to detect NaN values, never a direct equality comparison to np.nan

2. Combine np.isnan() with boolean indexing to filter out or replace missing/invalid data represented as NaN

3. Use np.nan_to_num() when you need to replace NaN, and optionally inf, values with a specific substitute in one call

⚠️

Tip: Never compare an array directly for equality against np.nan to detect NaN values — since NaN never equals anything, including itself, that comparison always evaluates to False even where NaN is genuinely present; use np.isnan(arr) instead.

editor.html
import numpy as np

arr = np.array([1.0, np.nan, 3.0])
print(np.isnan(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1.0, np.nan, 3.0])
print(np.isnan(arr))
Example 02Advanced Example
import numpy as np

arr = np.array([1.0, np.nan, 3.0, np.nan])
print(arr == np.nan)
print(np.isnan(arr))

Best Practices

  • Always use np.isnan(arr) to detect NaN values, never a direct equality comparison to np.nan
  • Combine np.isnan() with boolean indexing to filter out or replace missing/invalid data represented as NaN
  • Use np.nan_to_num() when you need to replace NaN, and optionally inf, values with a specific substitute in one call

Interview Question

Why does comparing np.nan to np.nan with == evaluate to False?

Hint: This is a deliberate part of the IEEE-754 floating-point standard, not a NumPy-specific quirk.

The IEEE-754 floating-point standard specifically defines NaN as unequal to every value, including another NaN, so that any computation producing an invalid or undefined result reliably fails equality checks rather than silently comparing equal to something meaningful. This is a deliberate design choice adopted across virtually every programming language that follows the standard, not a NumPy-specific behavior, and it's exactly why detecting NaN requires a dedicated function like isnan() rather than a direct equality comparison.

Exercises

MediumPractice using np.isnan() in a real scenario.
View Solution
import numpy as np

arr = np.array([1.0, np.nan, 3.0])
print(np.isnan(arr))

Frequently Asked Questions

Why does comparing np.nan to np.nan with == evaluate to False?

The IEEE-754 floating-point standard specifically defines NaN as unequal to every value, including another NaN, so that any computation producing an invalid or undefined result reliably fails equality checks rather than silently comparing equal to something meaningful. This is a deliberate design choice adopted across virtually every programming language that follows the standard, not a NumPy-specific behavior, and it's exactly why detecting NaN requires a dedicated function like isnan() rather than a direct equality comparison.

Related Functions

nonetypenp-isfinitenp-divide