assert_array_equal does not compare NaNs as numbers when dtype is object
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.
@rossbar added this feature here : https://github.com/numpy/numpy/pull/16128
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.
ohh right! thanks for the clarification.
Removed the Close? label, as adding the equal_nan keyword to np.testing.assert_array_equal still seems useful.