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

Christopher Barker Chris.Barker at noaa.gov
Fri Mar 20 16:39:38 EDT 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)
False

which makes sense, as:

 >>> np.nan == np.nan

False


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)

-Chris





-- 
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