0

I am building a simple null identifier function for identifying nan in Numpy arrays (not Pandas DataFrames or Series), but my code does not seem to work, even though after going over it multiple times I still see no problems with the syntax. What you guys think? Thank you The array:

X = np.array([[1,2,4,np.nan],[3,np.nan,4,2],[5,1,6,7],[np.nan,1,2,9]])

The iteration:

for i in range(X.shape[0]):
    for j in range(X.shape[1]):
        if X[i,j] == np.nan:
            print("This entry is null")
        else:
            print("This one is not")

It returns: "This one is not" sixteen times (the size of the array). In other words, no missing values apparently. What's wrong?

  • 1
    It is discussed more in other [SO questions](https://stackoverflow.com/questions/41342609/the-difference-between-comparison-to-np-nan-and-isnull). – Ali_Sh Sep 10 '21 at 19:16

1 Answers1

1

It is noted in numpy documentation cannot use equality to test NaNs by such modules. You must use np.isnan as follows:

for i in range(len(X)):
    for j in range(X.shape[1]):
        if np.isnan(X[i, j]):
            print("This entry is null")
        else:
            print("This one is not")
Bart
  • 9,825
  • 5
  • 47
  • 73
Ali_Sh
  • 2,667
  • 3
  • 43
  • 66