Why Does Comparing To Nan Yield False (python)?
Solution 1:
The creators of numpy
decided that it made most sense that most comparisons to nan
, including ==
, should yield False
. You can do this in Python by defining a __eq__(self, other)
method for your object. This behaviour was chosen simply because it is the most useful, for various purposes. After all, the fact that one entry has a missing value, and another entry also has a missing value, does not imply that those two entries are equal. It just implies that you don't know whether they are equal or not, and it's therefore best not to treat them as if they are (e.g. when you join two tables together by pairing up corresponding rows).
is
on the other hand is a Python keyword which cannot be overwritten by numpy
. It tests whether two objects are the same thing. nan
is the same object as nan
. This is also useful behaviour to have anyway, because often you will want to e.g. get rid of all entries which don't have a value, which you can achieve with is not nan
.
nan in (nan,)
returns True because as you probably know, (nan,)
is a tuple with only one element, nan
, and when Python checks if an object is in
a tuple, it is checking whether that object is
or
==
any object in the tuple.
Post a Comment for "Why Does Comparing To Nan Yield False (python)?"