[Numpy-discussion] NaN as dictionary key?

Wes McKinney wesmckinn@gmail....
Mon Apr 20 09:42:22 CDT 2009

I assume that, because NaN != NaN, even though both have the same hash value
(hash(NaN) == -32768), that Python treats any NaN double as a distinct key
in a dictionary.

In [76]: a = np.repeat(nan, 10)

In [77]: d = {}

In [78]: for i, v in enumerate(a):
   ....:     d[v] = i

In [79]: d
{nan: 0,
 nan: 1,
 nan: 6,
 nan: 4,
 nan: 3,
 nan: 9,
 nan: 7,
 nan: 2,
 nan: 8,
 nan: 5}

I'm not sure if this ever worked in a past version of NumPy, however, I have
code which does a "group by value" and occasionally in the real world those
values are NaN. Any ideas or a way around this problem?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090420/24c16c95/attachment.html 

More information about the Numpy-discussion mailing list