Check if matrix contains only specific values in numpy
I have large matrices that contain both real numbers and nan. I want to find the most efficient way to determine if a matrix contains only zeros and/or nan entries. If it does, the code should return True. Here is my MWE using numpy's all function
import numpy as np
# some large matrix
m = np.array([[0,3,4,5], [1,2,0,0], [0,0,np.nan,2], [np.nan,3,3,np.nan]])
# unique values
m0 = np.unique_all(m).values
# another large matrix
n = np.array([[0,np.nan,0,0], [0,np.nan,0,0], [0,0,np.nan,0], [np.nan,np.nan,np.nan,np.nan]])
n0 = np.unique_all(n).values
print(np.all((m0 == 0) & (m0 == np.nan))) # should be False
print(np.all((n0 == 0) & (n0 == np.nan))) # should be True
The results are
>> False
>> False
but should be
>> False
>> True
What am I doing wrong?