[Numpy-discussion] comparing arrays with NaN in them.

Christopher Barker Chris.Barker at noaa.gov
Thu Aug 23 20:34:20 EDT 2007


Hi all,

I was just trying to write a unit test for something where I was 
expecting to get some NaN's in the array. However, since NaN == NaN 
returns false, the simple test:

assert(alltrue(a == b))

 >>> a = N.array((1,2,3,N.nan))
 >>> b = N.array((1,2,3,N.nan))
 >>> a == b
array([ True,  True,  True, False], dtype=bool)

 >>> assert(N.alltrue(a == b))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AssertionError
 >>>

So is there any way to test is two arrays are the same, when there may 
be a NaN or two mixed in???

With a bit of thought -- this works:
 >>> N.alltrue(a[~N.isnan(a)] == b[~N.isnan(b)])
True

but that feels like a kludge. maybe some sort of "TheseArrays are binary 
equal" would be useful.

-CHB


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list