Skip to main content

A dictionary and counter package that only allows a single NaN instance key

Project description

This is not the NaN you are looking for

Alternate title: "NaN - More than Not a Number"

  • Python oddities concerning NaN
  • Live demo a dict that allows use of NaN as a key the way it would be intuitively, so that all NaN values are treated as one.
  • Live demo a Counter that counts how many occurences of NaN exist

Questions

  • What about copy.copy?

Description

Have you ever wondered about float('NaN')? What it is? What you can do with it? What you shouldn't do with it? Let's take some time and explore this Not a Number and what it does (and doesn't do) in Python.

Outline

  • Introduction. 3 minutes
    • Who am I?
    • What am I going to talk about?
    • What led me to write this talk?
  • Introducing NaN. 3 minutes
    • What is NaN?
    • What purpose does NaN serve?
    • The differences beteween float('nan'), numpy.nan, and math.nan.
    • How we can tell if a "numeric" value is NaN.
  • What makes NaN so unique? 5 minutes
    • No 2 instances of NaN are the same.
    • All instances of NaN have the same repr: "nan"
    • NaN is not equal to anything, not even itself.
    • NaN can be hashed but each instance of NaN will have a different hash.
      • A set can contain multiple instances of NaN.
      • A dictionary can have multiple NaN keys, but good luck accessing the data.
      • If you have an Iterable with multiple instances of NaN and create a Counter from it, each instance will appear with a count of 1.
  • Creating a Pythonic dict that can only have one NaN. 12 minutes
    • Do a code walkthrough and live demo of a Python subclass of dict that can only have one instance of NaN.
      • Show how I created the new version of dict.
      • Simple live demo showing that we can use float('nan') as a dict key. In a regular dict it will give a KeyError when attempting to retrieve it; with the new version of dict it will work as expected.
    • Do a code walkthrough and live demo of a Python Counter class that will count how many instances of NaN are in it.
      • Show how I created a Counter class that will count all instances of NaN.
      • Live demo showing the behaviour of collections.Counter and the new Counter class.
  • Conclusion. 2 minutes
    • Quick recap of what was discussed.
    • Where to find the talk slides.
    • Where to find me.

Things I've found

>>> float('nan') ** 0
1.0
>>> float('nan') > 1
False
>>> float('nan') < 1
False
>>> float('nan') == 1
False
>>> float('nan') != 1
True
>>>> float('inf') - float('inf')
nan
>>>> f'ba{1e234567 - 1e765432}as'
'bananas'
import struct
float_bytes = b"\xaa\xbb\xcc\xdd\xff\xff\xff\xff"
val = struct.unpack("d", float_bytes)[0]
print(val)
print(struct.pack("d", val))

In a single precision floating point number (32 bit) there are 2^24 - potential NaN values.

Quotes

NaN is a lot like infinity in that people want to treat it as a number but It's Not and trying to do that will only lead to surprise and disappointment. - Luna Celste

NaN is a wild land governed by laws both alien and arcane - Luna Celeste

References

https://xkcd.com/851/

https://brassnet.biz/blog/nan-is-weird.html

https://github.com/python/cpython/issues/87641

https://twit.social/@brass75/116168712645625711

https://discuss.python.org/t/question-about-float-nan/106378

https://en.wikipedia.org/wiki/NaN

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nandict-2026.6.1.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nandict-2026.6.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file nandict-2026.6.1.tar.gz.

File metadata

  • Download URL: nandict-2026.6.1.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nandict-2026.6.1.tar.gz
Algorithm Hash digest
SHA256 0b14635b048f14268795acbcfa165a639d0c8a252c2d71947120faa11246a875
MD5 69c588886960e0a00fe89d6d5a7a34f0
BLAKE2b-256 e0d92a5d8d5770b9af1cd473084a98d247be8d1a281a99b996b1eece68d6f4fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for nandict-2026.6.1.tar.gz:

Publisher: publish.yml on brass75/nan-talk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nandict-2026.6.1-py3-none-any.whl.

File metadata

  • Download URL: nandict-2026.6.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nandict-2026.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 571c2a9f031119dad504668855b2a96e450018afb64be68e5be6e2ece14db7bb
MD5 5113aece342b409b5551a648a3fbf455
BLAKE2b-256 370604a1a74bd68c1d111a660b45b109b8121f20020693661a39c7613bb2d45d

See more details on using hashes here.

Provenance

The following attestation bundles were made for nandict-2026.6.1-py3-none-any.whl:

Publisher: publish.yml on brass75/nan-talk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page