Handling NaN & Infinity

NaN ("Not a Number") and infinity are special floating-point values NumPy uses to represent missing or undefined data, and learning to detect and clean them is essential for trustworthy numeric results.

Learn Handling NaN & Infinity in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll discover why NaN never equals itself, detect it with np.isnan and friends, compute around it using nan-aware reductions, and replace it with np.nan_to_num.

np.nan is a special float meaning "missing or undefined", and np.inf represents infinity (often from dividing by zero). The surprising part: NaN never equals anything, not even itself , so np.nan == np.nan is False . That single fact is why you cannot find NaN with == .

Three element-wise helpers give you boolean masks. np.isnan flags NaN, np.isinf flags positive or negative infinity, and np.isfinite flags the "good" values that are neither. Combine them with boolean indexing to count or filter.

Ordinary sum and mean propagate NaN — one missing value poisons the whole result. The fix is the nan* family: np.nansum , np.nanmean , np.nanmax , and friends simply skip NaN. To replace the bad values entirely, np.nan_to_num swaps NaN for 0 and infinities for large finite numbers.

Replace ___ with the function that computes a mean while ignoring the missing value.

Answer: nanmean — it averages 10, 20, 30 (skipping NaN) to get 20.0.

✅ Fix: use the dedicated mask arr[np.isnan(arr)] .

✅ Fix: use the nan-aware reduction np.nanmean(arr) to skip NaN.

NaN is a float; you cannot store it in an int array.

✅ Fix: create the array as float, e.g. np.array([1, 2], dtype=float) , before inserting NaN.

A sensor stream has a missing reading. Count the missing values, compute the mean of the valid ones, then build a cleaned array where NaN is replaced by that mean.

Lesson complete — missing data, handled!

You know why nan != nan , detect special values with isnan / isinf / isfinite , reduce safely with the nan* family, and clean arrays with np.nan_to_num .

🚀 Up next: Unique Values & Counting — find distinct values and tally how often each appears.

Practice quiz

What does np.nan represent?

  • A missing or undefined value
  • Zero
  • The largest integer
  • An empty string

Answer: A missing or undefined value. np.nan ('Not a Number') marks missing or undefined floating-point data.

What does the comparison np.nan == np.nan evaluate to?

  • True
  • False
  • nan
  • An error

Answer: False. NaN never equals anything, including itself, so the result is False.

Which function flags positive or negative infinity?

  • np.isnan
  • np.isfinite
  • np.isinf
  • np.nan_to_num

Answer: np.isinf. np.isinf returns True wherever a value is positive or negative infinity.

What does np.isfinite([1.0, np.nan, 3.0, np.inf, 5.0]) return?

isfinite is True only for the non-NaN, non-inf values: positions 0, 2, 4.

What does 1 / np.inf evaluate to?

  • nan
  • inf
  • 1.0
  • 0.0

Answer: 0.0. Dividing a finite number by infinity gives 0.0.

Why does arr[arr == np.nan] find nothing?

  • NaN is never equal to itself, so the mask is all False
  • arr is empty
  • It raises an error
  • == rounds the values

Answer: NaN is never equal to itself, so the mask is all False. Because NaN != NaN, the mask is all False; use np.isnan instead.

What does np.nansum([1.0, 2.0, np.nan, 4.0]) return?

  • nan
  • 7.0
  • 6.0
  • 4.0

Answer: 7.0. nansum skips the NaN and adds 1+2+4 = 7.0.

What does np.nan_to_num([1.0, 2.0, np.nan, 4.0]) return by default?

nan_to_num replaces NaN with 0 by default: [1. 2. 0. 4.].

Why can't you store NaN in an integer array?

  • NaN is a float-only value
  • Integers are read-only
  • NaN is a string
  • Arrays forbid zeros

Answer: NaN is a float-only value. NaN is a floating-point value, so the array must be float dtype to hold it.

Which comparison is True?

  • np.nan == 0
  • np.nan != np.nan is False
  • -np.inf < -1e300
  • 1 / np.inf > 0

Answer: -np.inf < -1e300. Negative infinity is less than any finite number, so -np.inf < -1e300 is True.