Skip to main content

pandas isna()

If you try to check for np.nan or None using pandas isna() to get a bool, that is either True or False. What if we want to get not nan cases? Well, we can add a not before or a common other solution is a bitwise Not Operator ~. What you would expect is getting the True for False and False for True. That’s true for the case of not and it’s not true for ~.

>>> pd.isna(None)
True
>>> not pd.isna(None)
False
>>> ~pd.isna(None)
-2

What happened, why did we get -2?

As it turns out ~pd.isna(None) when exposed to bitwise operation is taken not as a bool but a number representation of True which is 1. What does it mean to apply a bitwise not on number one?

$$ \begin{aligned} \mathtt{NOT} \quad &0000\;0000\;0000\;0001 \; \mathsf{(decimal 1)} \\ =\quad &1111\;1111\;1111\;1110 \; \mathsf{(decimal -2)} \end{aligned} $$

That’s from where -2 came from.