numpy icon indicating copy to clipboard operation
numpy copied to clipboard

assert_array_equal does not compare NaNs as numbers when dtype is object

Open cwfinn opened this issue 9 years ago • 4 comments

OS: macOS Sierra (10.12.5) Python version: 2.7.12 numpy version: 1.13.0

I notice that numpy.testing.assert_array_equal should compare NaNs as numbers unlike elsewhere in numpy, which makes sense to me. I verify this with the following test:

>>> import numpy.testing as npt
>>> import numpy as np
>>> a = np.array([1, 2, np.nan])
>>> npt.assert_array_equal(a, a)

However, this does not work with arrays that have dtype object:

>>> b = np.array([1, 2, 'a', np.nan], dtype=object)
>>> npt.assert_array_equal(b, b)
AssertionError: 
Arrays are not equal

(mismatch 25.0%)
 x: array([1, 2, 'a', nan], dtype=object)
 y: array([1, 2, 'a', nan], dtype=object)

Is this the desired behaviour? The only workaround in my case is to cast the array as dtype str.

cwfinn avatar Jun 29 '17 15:06 cwfinn

@rossbar added this feature here : https://github.com/numpy/numpy/pull/16128

anirudh2290 avatar May 30 '20 01:05 anirudh2290

Thanks for the xref @anirudh2290, just a quick clarification - the feature added in #16128 affected np.array_equal, not np.testing.assert_array_equal. The reference is useful though as there was some related discussion in #9229, which was closed by #16128.

rossbar avatar May 30 '20 02:05 rossbar

ohh right! thanks for the clarification.

anirudh2290 avatar May 30 '20 03:05 anirudh2290

Removed the Close? label, as adding the equal_nan keyword to np.testing.assert_array_equal still seems useful.

rgommers avatar Nov 21 '22 10:11 rgommers