pythonnumpynanequality-operator

Comparing numpy arrays containing NaN


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):
    print 'arrays are equal'

This does not work because nan != nan. What is the best way to proceed?


Solution

  • Alternatively you can use numpy.testing.assert_equal or numpy.testing.assert_array_equal with a try/except:

    In : import numpy as np
    
    In : def nan_equal(a,b):
    ...:     try:
    ...:         np.testing.assert_equal(a,b)
    ...:     except AssertionError:
    ...:         return False
    ...:     return True
    
    In : a=np.array([1, 2, np.NaN])
    
    In : b=np.array([1, 2, np.NaN])
    
    In : nan_equal(a,b)
    Out: True
    
    In : a=np.array([1, 2, np.NaN])
    
    In : b=np.array([3, 2, np.NaN])
    
    In : nan_equal(a,b)
    Out: False
    

    Edit

    Since you are using this for unittesting, bare assert (instead of wrapping it to get True/False) might be more natural.