Why does NaN != NaN evaluate to True, and what are the implications for comparing or checking floating-point arrays?

Quick Overview

Explain why NaN compares unequal to itself under IEEE 754 floating-point rules and what that means when you search, compare or test NumPy arrays. It tests your knowledge of NaN comparison semantics, np.isnan, equal_nan options, NaN-aware aggregations and common pitfalls in pandas.

Why does NaN != NaN evaluate to True, and what are the implications for comparing or checking floating-point arrays?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Why does `NaN != NaN` evaluate to `True`, and what are the implications for comparing or checking floating-point arrays? ```hint Start from the standard Think about what NaN is meant to represent and what the floating-point standard says every comparison involving it should return. ``` ### What a Strong Answer Covers - Why IEEE 754 makes NaN unequal to every value, itself included. - What each comparison operator returns when one side is NaN. - Why equality-based checks fail on arrays, and the right tools to detect NaN and to compare arrays that contain it. - Knock-on effects in everyday code: aggregations, sorting, membership tests, deduplication or unit tests. ### Follow-up Questions - Why does `np.nan in [np.nan]` return `True` while `np.nan in np.array([np.nan])` returns `False`? - How do `np.max` and `np.nanmax` differ, and when do you want NaN to propagate? - How would you write a unit test that compares two float arrays containing NaN? - What happens in pandas when you group by or join on a column that contains NaN?

Overview: Explain why NaN compares unequal to itself under IEEE 754 floating-point rules and what that means when you search, compare or test NumPy arrays. It tests your knowledge of NaN comparison semantics, np.isnan, equal_nan options, NaN-aware aggregations and common pitfalls in pandas.

PracHub logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Why does NaN != NaN evaluate to True, and what are the implications for comparing or checking floating-point arrays?

What a Strong Answer Covers Guidance

  • Why IEEE 754 makes NaN unequal to every value, itself included.
  • What each comparison operator returns when one side is NaN.
  • Why equality-based checks fail on arrays, and the right tools to detect NaN and to compare arrays that contain it.
  • Knock-on effects in everyday code: aggregations, sorting, membership tests, deduplication or unit tests.

Follow-up Questions Guidance

  • Why does np.nan in [np.nan] return True while np.nan in np.array([np.nan]) returns False ?
  • How do np.max and np.nanmax differ, and when do you want NaN to propagate?
  • How would you write a unit test that compares two float arrays containing NaN?
  • What happens in pandas when you group by or join on a column that contains NaN?