Ray's Knowledge Base
Unlock

Deduping floats by value bits changes unique and nunique results with NaN

PitfallVerified 4 Oct 2026Holds project: dftracer-utils
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

unique and nunique on floats return different results after the dedupe is keyed by value bits. The first id-based unique turned unique([NaN, NaN]) from two NaNs into one.

Cause#

The old path sorts every row and compares with NaN != NaN, so each NaN row is its own value. It also keeps whichever of 0.0 and -0.0 the sort puts first. Ids merge equal bit patterns, so NaNs merge and the other zero may be kept.

Fix#

A speed-up of unique or nunique must not change results. If any distinct value of a float column is NaN (checked only on the few distinct values), unique and nunique run the old full path; only float columns that hold a NaN pay one extra scan. Other columns use value ids. Rejected: accepting the merged NaN as a harmless side effect.

Pin the HEAD output (run the old build on the same input) in a test. Comparing new code on a layout with new code on its flat copy cannot catch this, and a single NaN in the input does not show the difference: use two non-null NaNs (old output nan nan 0 1 7).

Evidence#

dftracer-utils distinct-on-ids work, 2026-10-04.

Also found by searching

  • float unique nunique result changes after switching to value-id dedupe
  • unique([NaN, NaN]) returns one NaN instead of two
  • NaN rows each count as their own value in unique
  • test passes comparing layout to flat copy but unique output changed