The tilde operator in Python
It is a unary operator (taking a single argument) that is borrowed from C, where all data types are just different ways of interpreting bytes. It is the "invert" or "complement" operation, in which all the bits of the input data are reversed.
In Python, for integers, the bits of the twos-complement representation of the integer are reversed (as in b <- b XOR 1
for each individual bit), and the result interpreted again as a twos-complement integer. So for integers, ~x
is equivalent to (-x) - 1
.
The reified form of the ~
operator is provided as operator.invert
. To support this operator in your own class, give it an __invert__(self)
method.
>>> import operator
>>> class Foo:
... def __invert__(self):
... print 'invert'
...
>>> x = Foo()
>>> operator.invert(x)
invert
>>> ~x
invert
Any class in which it is meaningful to have a "complement" or "inverse" of an instance that is also an instance of the same class is a possible candidate for the invert operator. However, operator overloading can lead to confusion if misused, so be sure that it really makes sense to do so before supplying an __invert__
method to your class. (Note that byte-strings [ex: '\xff'
] do not support this operator, even though it is meaningful to invert all the bits of a byte-string.)
Applications of '~' (tilde) operator in Python
The standard use cases for the bitwise NOT operator are bitwise operations, just like the bitwise AND &
, the bitwise OR |
, the bitwise XOR ^
, and bitwise shifting <<
and >>
. Although they are rarely used in higher level applications, there are still some times where you need to do bitwise manipulations, so that’s why they are there.
Of course, you may overwrite these for custom types, and in general you are not required to follow any specific semantics when doing so. Just choose what makes sense for your type and what still fits the operator in some way.
If the operation is obscure and better explained with a word or two, then you should use a standard method instead. But there are some situations, especially when working with number related types, that could have some mathematical-like operations which fit the bitwise operators, and as such are fine to use those.
Just like you would overwrite standard operators like +
and -
only for meaningful operations, you should try to do the same for bitwise operators.
The reason ~~True, ~~False
gives you (1, 0)
is because the bool
type does not define its own __invert__
operation. However, int
does; and bool
is actually a subtype of int
. So bool
actually inherits the logic of all bitwise and arithmetical operators. That’s why True + True == 2
etc.
Tilde sign in pandas DataFrame
It means bitwise not, inversing boolean mask - False
s to True
s and True
s to False
s.
Sample:
df = pd.DataFrame({'InvoiceNo': ['aaC','ff','lC'],
'a':[1,2,5]})
print (df)
InvoiceNo a
0 aaC 1
1 ff 2
2 lC 5
#check if column contains C
print (df['InvoiceNo'].str.contains('C'))
0 True
1 False
2 True
Name: InvoiceNo, dtype: bool
#inversing mask
print (~df['InvoiceNo'].str.contains('C'))
0 False
1 True
2 False
Name: InvoiceNo, dtype: bool
Filter by boolean indexing
:
df = df[~df['InvoiceNo'].str.contains('C')]
print (df)
InvoiceNo a
1 ff 2
So output is all rows of DataFrame, which not contains C
in column InvoiceNo
.
What does the ~ operator do in Python
~
is a unary operator (i.e. it takes only one argument) which calculates the bitwise inverse of its argument. The result is -x - 1
, because in Two's complement representation, -x
is the same as inverting all bits and then adding one.
What does the squiggle (tilde) i.e. `~` operator do in Python?
It's the unary bitwise invert operator.
The unary
~
(invert) operator yields the bitwise inversion of its plain or long integer argument. The bitwise inversion ofx
is defined as-(x+1)
. It only applies to integral numbers.
What does ~ do in python besides being a bitwise negation op?
Check the class being 'inverted' has a __invert__(self)
method, if so, that's overriding the tilde operator.
Cracking my head over the usage of tilde in Pandas
To work with the ~
operator you first need to generate a DataFrame like for example:
import pandas as pd
mask = [False, False, False, False, True, True, False]
mask = pd.DataFrame(mask, columns=['mask'])
mask = ~mask
print(mask)
Output:
mask
0 True
1 True
2 True
3 True
4 False
5 False
6 True
If you want directly via the list, do a list comprehension working with not
:
mask = [False, False, False, False, True, True, False]
mask = [not i for i in mask]
print(mask)
Output:
[True, True, True, True, False, False, True]
Where is official documentation for tilde (~) in Pandas?
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:|
foror
,&
forand
, and~
fornot
. These
must be grouped by using parentheses.
Bold emphasis mine.
Related Topics
How to Get the Pythonpath in Shell
Pandas Get Topmost N Records Within Each Group
Getting a List of Values from a List of Dicts
What Are "Named Tuples" in Python
What Should I Do with "Unexpected Indent" in Python
Matplotlib Different Size Subplots
What Exactly Is a "Raw String Regex" and How to Use It
"Nameerror: Name '' Is Not Defined" After User Input in Python
Matplotlib (Pyplot) Savefig Outputs Blank Image
How to Get Method Parameter Names
Fastest Way to Download 3 Million Objects from a S3 Bucket
How to Make a Discontinuous Axis in Matplotlib
How to Keep Python Print from Adding Newlines or Spaces
Your CPU Supports Instructions That This Tensorflow Binary Was Not Compiled to Use: Avx Avx2
Fast Punctuation Removal with Pandas
Best Way to Replace Multiple Characters in a String
Typeerror: Unsupported Operand Type(S) for -: 'Str' and 'Int'