comparing numpy arrays containing NaN

后端 未结 7 1578
情话喂你
情话喂你 2020-11-28 09:27

For my unittest, I want to check if two arrays are identical. Reduced example:

a = np.array([1, 2, np.NaN])
b = np.array([1, 2, np.NaN])
if np.all(a==b):
          


        
7条回答
  •  感动是毒
    2020-11-28 10:09

    If you do this for things like unit tests, so you don't care much about performance and "correct" behaviour with all types, you can use this to have something that works with all types of arrays, not just numeric:

    a = np.array(['a', 'b', None])
    b = np.array(['a', 'b', None])
    assert list(a) == list(b)
    

    Casting ndarrays to lists can sometimes be useful to get the behaviour you want in some test. (But don't use this in production code, or with larger arrays!)

提交回复
热议问题