numpy NaN, not surviving pickle/unpickle?

Christian Heimes lists at cheimes.de
Mon Sep 14 00:52:48 CEST 2009


John Ladasky wrote:
> OK, so there's a dedicated function in numpy to handle this.  Thanks!
> 
> I tried "x is NaN" after noting the obvious, that any equality or
> inequality test involving NaN will return False.
> 
> In my leisure time, I would like to dig deeper into the issue of why
> object identities are not guaranteed for elements in numpy arrays...
> with elements of type "float", at least, I thought this would be
> trivial.

You mustn't use "is" on numbers ever. It might work under some special
circumstances but in general it doesn't do what you expect. NaN isn't a
singleton in Python. Python's float type uses IEEE 754 double precision
numbers internally. The double type has much more than billions of NaN
values (IIRC 2**53). isnan() is the only reliable way to detect NaNs. "x
!= x" is a hack that works on most platforms, too.

Christian




More information about the Python-list mailing list