+ 2

Why does "np.NaN!=np.NaN" ?

In the Data Science course under the missing numbers project, they use np.NaN. If they discuss that elsewhere in the course I missed it. My code didn't work and I ended up realizing that not matter how I wrote it, "print(np.NaN!=np.NaN)" always returned "True". Switching to None caused as error as the final array was of type Object rather than Float and I couldn't get astype() to work. Eventually, I just got fed up and defined a variable nan as a random float and my code worked as expected. While I now know there is a np.isnan exists, I would like to know what happened. *Edit* I understand now that it is defined to not be equal. Any insight as to why it was defined that way?

12th Oct 2021, 8:26 PM
Jon
2 Answers
+ 5
Jon You cannot use equality test on NaN. Also nan, NaN and NAN are all aliases of the same thing. See the following for more detail https://numpy.org/doc/stable/user/misc.html?highlight=numpy%20nan
12th Oct 2021, 8:55 PM
Paul K Sadler
Paul K Sadler - avatar
+ 4
nan is defined to not equal itself, therefore isnan() to check if a valie is not a number.
12th Oct 2021, 9:02 PM
Lisa
Lisa - avatar