2

I am pretty sure that ~ in Pandas is boolean not. I found a couple of StackOverflow questions / answers, but no pointer to official documentation.

Sanity Check

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd


df = pd.DataFrame([(1, 2, 1),
                   (1, 2, 2),
                   (1, 2, 3),
                   (4, 1, 612),
                   (4, 1, 612),
                   (4, 1, 1),
                   (3, 2, 1),
                   ],
                  columns=['groupid', 'a', 'b'],
                  index=['India', 'France', 'England', 'Germany', 'UK', 'USA',
                         'Indonesia'])

print(df)
filtered = df[~(df['a'] == 2)]
print(filtered)

The df is

           groupid  a    b
India            1  2    1
France           1  2    2
England          1  2    3
Germany          4  1  612
UK               4  1  612
USA              4  1    1
Indonesia        3  2    1

and filtered is

         groupid  a    b
Germany        4  1  612
UK             4  1  612
USA            4  1    1

So I'm pretty sure it is boolean not.

Joe
  • 41,484
  • 20
  • 104
  • 125
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

4 Answers4

11

The ~ is the operator equivalent of the __invert__ dunder which has been overridden explicitly for the purpose performing vectorized logical inversions on pd.DataFrame/pd.Series objects.

s = pd.Series([True, False])

~s

0    False
1     True
dtype: bool

s.__invert__()

0    False
1     True
dtype: bool

Note: Dunder methods must not be used directly in code, always prefer the use of the operators.

Also, since you've asked, the section on Boolean Indexing describes its use.

Another common operation is the use of boolean vectors to filter the data. The operators are: | for or, & for and, and ~ for not. These must be grouped by using parentheses.

Bold emphasis mine.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • Nice explanation. However I believe this operator is overridden from standard python [__invert__](https://docs.python.org/3/reference/datamodel.html#object.__invert__) to support vectorised behaviour. Similar to the rest of pandas operators. – Sohaib Farooqi Mar 12 '18 at 14:13
  • Thanks, that terminology needed fixing. – cs95 Mar 12 '18 at 14:14
1

I found it referenced on this page. It's about halfway down, I'd navigate to it with ctrl+F. You're correct though, it's the not operator.

cs95
  • 379,657
  • 97
  • 704
  • 746
M Dillon
  • 164
  • 1
  • 8
1

Here they define explicitly:

Another common operation is the use of boolean vectors to filter the data. The operators are: | for or, & for and, and ~ for not. These must be grouped by using parentheses.

zipa
  • 27,316
  • 6
  • 40
  • 58
0

If you go to: https://docs.python.org/3/library/operator.html, it says:

Bitwise Inversion   ~ a invert(a)
TYZ
  • 8,466
  • 5
  • 29
  • 60
  • But does Pandas overwrite `~` with a magic method? I guess that is possible with [`__neg__`](https://docs.python.org/3/reference/datamodel.html#object.__neg__)? – Martin Thoma Mar 12 '18 at 14:06