numpy NaN, not surviving pickle/unpickle?

John Ladasky john_ladasky at
Sun Sep 13 22:00:38 CEST 2009

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.

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.  Here's
a minimal sample program, and its output:

=== program ========================================

## numpy nan pickle

import pickle
from numpy import *

print "\n\nNaN equivalency tests:\n"
x, y = nan, NaN  # Capitalization reality check
print "x =", x, ", y =", y
print "x is nan:", x is nan
print "y is NaN:", y is NaN
print "x is y:", x is y

A0 = array([[1.2, nan], [3.4, 5.6]])
print "\n\nPickling and saving this array to disk:\n\n", A0
f0 = open("test array", "w")
pickle.dump(A0, f0)
print "\nArray saved to disk."

f1 = open("test array", "r")
A1 = pickle.load(f1)
print "\n\nThe array reloaded from the disk is:\n\n", A1
print "\narray[0,1] =", A1[0,1]
print "array[0,1] is nan:", A1[0,1] is nan, "\n\n"

=== output ========================================

NaN equivalency tests:

x = nan , y = nan
x is nan: True
y is NaN: True
x is y: True

Pickling and saving this array to disk:

[[ 1.2  NaN]
 [ 3.4  5.6]]

Array saved to disk.

The array reloaded from the disk is:

[[ 1.2  NaN]
 [ 3.4  5.6]]

array[0,1] = nan
array[0,1] is nan: False


The last line of my output is unexpected.  I've printed the contents
of the cell in the array, and it says that it contains "nan".  But
when I try the same equivalency test that I tried in the first few
lines of the program (with unpickled objects), this time it says that
my test object isn't "nan".

I thought that Python was supposed to make values and even objects

Obligatory version information:

Numpy: 1.0.4
Python: 2.5.2
OS: Ubuntu Linux 8.04

Thanks for any help!

More information about the Python-list mailing list