Happy for this to be a wontfix but I'm trying to understand the logic behind it. Suppose I have some Series instances
import pandas as pd
a = pd.Series([True,False,True])
b = pd.Series([0, 1, 2])
and I try to perform a logical not
using the ~
operator. Then I get
>>> print(~a)
0 False
1 True
2 False
dtype: bool
>>> print(~b)
0 -1
1 -2
2 -3
dtype: int64
The first example makes sense because I'm expecting to perform a vector boolean not. The second case makes sense if it's interpreted as applying bitwise not
to every element, e.g.
>>> ~1
-2
However, the reason this behaviour is confusing is because in the case where the Series dtype is 'bool', it does not seem to be simply applying bitwise not
to each element
>>> ~True
-2
>>> ~False
-1
My expectation would have been that the ~
operator on the Series class would consistently function as boolean not
, so the Series would be automatically converted to 'bool' first if it was a different type. That is, ~b
would produce the same output as ~(b.astype('bool'))
. This is indeed the approach that Matlab takes. I'm not sure if there's a specific reason for the current implementation, but it does seem confusing that the same operator can change between boolean and bitwise operations depending on the dtype.
Comment From: jreback
this is how numpy does this. ~
is the same as -
so this is why you get this. I
In [4]: ~(a.values)
Out[4]: array([False, True, False], dtype=bool)
In [5]: ~(b.values)
Out[5]: array([-1, -2, -3])