[Numpy-discussion] using assertEqual in unittest to test two np.ndarray?

Christopher Barker Chris.Barker@noaa....
Fri Mar 20 15:39:38 CDT 2009

Grissiom wrote:
> I know I should use array_equal to test two arrays

Not answering your question, but I hadn't known about array_equal, so 
when I saw this, I thought: great! I can get rid of a bunch of ugly code 
in my tests. However, it doesn't work as I would like for NaNs:

 >>> a
array([  1.,   2.,  NaN,   4.])
 >>> b
array([  1.,   2.,  NaN,   4.])
 >>> np.array_equal(a,b)

which makes sense, as:

 >>> np.nan == np.nan


however, for my purposes, and for my tests, if two arrays have NaNs in 
the same places, and all the other values are equal, they should be 
considered equal. I guess my way of thinking about it is that:

np.array_equal(a, a)

should always return True

I understand that there are good reasons that NaN != NaN. However, it 
sure would be nice to have an array_equal that fit my purposes -- maybe 
a flag one could set?

np.array_equal(a1, a2, nan_equal=True)


Christopher Barker, Ph.D.

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


More information about the Numpy-discussion mailing list