0

As we know that like zero value is boolean False, my question is that why bool(np.nan) is True. Python version is 3.7

>>> import numpy as np
>>> bool(np.nan)
True
RayZen
  • 141
  • 1
  • 4
  • 1
    I think you should edit your code snipped. You asked why np.nan is True, but the snippet shows that it is False – awesoon Sep 04 '18 at 05:32
  • 1
    it is merely *convention* that the truth value of numeric types is *anything but 0 is True, and 0 is false*. `nan` is not `0` – juanpa.arrivillaga Sep 04 '18 at 06:08
  • @soon, I fix the fault. Sorry, I have no privilege to upload the picture. – RayZen Sep 04 '18 at 08:03
  • But, when we deal with the value nan, it looks like zero-behavior so much, maybe more like None. So can you give some more detailed information?thx @juanpa.arrivillaga – RayZen Sep 04 '18 at 08:10
  • @RayZen, numbers are not booleans and it is fine to perform additional checks while converting them to booleans. If you are dealing with them like `bool(val)` then you are most likely doing something wrong - at least from the business logic. Writing `val != 0` is more natural. And you can check for nan using `isnan` methods (numpy provides it, as well as standard library) – awesoon Sep 04 '18 at 08:20
  • @RayZen it may be like zero. One is also like zero in many ways, it is the multiplicative identity, for example, but one is not zero. NaN has properties different from zero as well, although some similar. But again, that is besides the point. It may or may not be similar to zero, but it is *not equal to zero*. Indeed, one unique fact about `nan` is that *it is not equal to anything, not even itself*; – juanpa.arrivillaga Sep 04 '18 at 08:34
  • ok, I got the point. Thanks for your tips – RayZen Sep 04 '18 at 12:22

0 Answers0