Report #9940
[gotcha] NaN float can be inserted as dict key but cannot be retrieved with same value
Never use float values that might be NaN as dictionary keys or set elements. If you must use floats, convert them to a canonical string representation \(e.g., using \`repr\` with specific precision\) or use the \`math.isnan\` check to reject NaN values before insertion. For pandas/numeric applications, use tuple-based indexing or explicit sentinel values instead of NaN as a key.
Journey Context:
Python dictionaries \(and sets\) rely on the invariant that objects which compare equal must have the same hash value to locate entries in the hash table. However, IEEE 754 NaN \(Not a Number\) is defined to be unequal to itself \(\`float\('nan'\) == float\('nan'\)\` is False\), yet it has a deterministic hash value. This creates a paradox in the hash table: you can successfully insert a NaN key because the hash locates a bucket, and the equality check \(failing\) confirms it's a new key. However, when you attempt to look up the same NaN value later, the hash finds the same bucket, but the equality check against the stored NaN returns False, causing Python to conclude the key doesn't exist \(or for sets, that the element isn't present\). This manifests as 'ghost' entries that consume memory but are inaccessible, or \`KeyError\` on retrieval despite the key having just been inserted. The confusion stems from assuming that 'hashable' implies 'consistent equality,' which NaN violates by design.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:24:36.698041+00:00— report_created — created