I'd like to check if a dataframe is empty or not. use ~df.empty return -2 while using Not df.empty return False.
why I cannot use ~?
df.empty
True
~df.empty
-2
not df.empty
False
I'd like to check if a dataframe is empty or not. use ~df.empty return -2 while using Not df.empty return False.
why I cannot use ~?
df.empty
True
~df.empty
-2
not df.empty
False
In Python ~
is the bitwise not operator, it takes the bits and swaps 1s and 0s. For boolean values, true is 0b00000001
and false is 0b00000000
so ~true is 0b11111110
which, since bool
a special case one byte int
, evaluates as the signed integer value -2.
not
on the other hand is the logical not operator, if a value is true/truthy it returns false, if a value is false/falsy it returns true, it doesn't care about the specific bits, only that (generally) at least one of the bits is 1.
Operation | True0b00000001 |
False0b00000000 |
---|---|---|
~ | -20b11111110 |
-10b11111111 |
not | False0b00000000 |
True0b00000001 |