numpy NaN, not surviving pickle/unpickle?

Robert Kern robert.kern at
Sun Sep 13 22:22:00 CEST 2009

John Ladasky wrote:
> Hi folks,
> I am aware that numpy has its own discussion group, which is hosted at
> gmane.  Unfortunately, I can't seem to get in to gmane today.

It is not hosted at GMane. It just has a GMane mirror.

> In any case, I'm not sure whether I have a problem with numpy, or with
> my understanding of the Python pickle module, so I'm posting here.
> I am pickling numpy.ndarray objects to disk which are of type "float",
> but which may include NaN in some cells.  When I unpickle these
> objects and then test for the presence of NaN, the test fails.

The problem is that you are trying to use "is" to compare by Python object 
identity. Except for dtype=object arrays, the object identities of the 
individual elements that you extract from numpy arrays are never guaranteed. 
Usually, they will always be different. You need to use numpy.isnan() to 
determine whether an object is a NaN.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-list mailing list