How can I obtain the element-wise logical NOT of a pandas Series? How can I obtain the element-wise logical NOT of a pandas Series? python python

How can I obtain the element-wise logical NOT of a pandas Series?


To invert a boolean Series, use ~s:

In [7]: s = pd.Series([True, True, False, True])In [8]: ~sOut[8]: 0    False1    False2     True3    Falsedtype: bool

Using Python2.7, NumPy 1.8.0, Pandas 0.13.1:

In [119]: s = pd.Series([True, True, False, True]*10000)In [10]:  %timeit np.invert(s)10000 loops, best of 3: 91.8 µs per loopIn [11]: %timeit ~s10000 loops, best of 3: 73.5 µs per loopIn [12]: %timeit (-s)10000 loops, best of 3: 73.5 µs per loop

As of Pandas 0.13.0, Series are no longer subclasses of numpy.ndarray; they are now subclasses of pd.NDFrame. This might have something to do with why np.invert(s) is no longer as fast as ~s or -s.

Caveat: timeit results may vary depending on many factors including hardware, compiler, OS, Python, NumPy and Pandas versions.


@unutbu's answer is spot on, just wanted to add a warning that your mask needs to be dtype bool, not 'object'. Ie your mask can't have ever had any nan's. See here - even if your mask is nan-free now, it will remain 'object' type.

The inverse of an 'object' series won't throw an error, instead you'll get a garbage mask of ints that won't work as you expect.

In[1]: df = pd.DataFrame({'A':[True, False, np.nan], 'B':[True, False, True]})In[2]: df.dropna(inplace=True)In[3]: df['A']Out[3]:0    True1   FalseName: A, dtype objectIn[4]: ~df['A']Out[4]:0   -20   -1Name: A, dtype object

After speaking with colleagues about this one I have an explanation: It looks like pandas is reverting to the bitwise operator:

In [1]: ~TrueOut[1]: -2

As @geher says, you can convert it to bool with astype before you inverse with ~

~df['A'].astype(bool)0    False1     TrueName: A, dtype: bool(~df['A']).astype(bool)0    True1    TrueName: A, dtype: bool


I just give it a shot:

In [9]: s = Series([True, True, True, False])In [10]: sOut[10]: 0     True1     True2     True3    FalseIn [11]: -sOut[11]: 0    False1    False2    False3     True