-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: Dangerous inconsistency: ~
operator changes behavior based on context outside a target.
#61598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
~
operator changes behavior based on context.~
operator changes behavior based on context outside the target.
~
operator changes behavior based on context outside the target.~
operator changes behavior based on context outside a target.
I believe the way to do what you want is simply ~((df['B'] > 3) & (df['C'] < 8) This keeps all the math within pandas and returns a boolean Series. When you call df.apply(lambda x: ~((x['B'] > 3) & (x['C'] < 8)), axis=1) you are expressly telling the system that you want to take each row of df, cast it to a Series, look up individual elements of the Series, and then apply ~ to the elements. In this case you are taking all the math away from pandas and sending it to Python, which is doing something you don't want, which is why you get the Python warning. On line 4 I think you happen to get away with it because the whole DataFrame is dtyped as np.int64 and so you're staying with numpy scalars (e.g. np.int64, rather than python int), and so your comparison operators return In[1]: ~np.bool_(False)
Out[1]: True
In [2]: ~False
Out[2]: -1 |
Sorry, your comment is unacceptable for me.
If such a curious behaviour is a 'correct specifiation' in pandas, pandas shoud never recommend |
Uh oh!
There was an error while loading. Please reload this page.
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
This ia reprot about
~
opetarotr in pandas dataframe.Here is the example on python=3.10.12, pandas=2.2.3.
In the above example, the same
df.apply(lambda x: ~((x['B'] > 3) & (x['C'] < 8)), axis=1)
is executed in step 4, 5, and 7.However, the result of step 7 is ridiculous.
In spite of
~
,not
operator returns a correct answer.It seems that
~
operator in pandas dataframe quite dangerous and unreliable.In the environment of python 3.13.3, panads=2.2.3, only for the step 7, python returns warning that
:1: DeprecationWarning: Bitwise inversion '~' on bool is deprecated and will be removed in Python 3.16. This returns the bitwise inversion of the underlying int object and is usually not what you expect from negating a bool. Use the 'not' operator for boolean negation or ~int(x) if you really want the bitwise inversion of the underlying int.
.However, I think this is a warning by python (not by pandas) from a different point of view.
Expected Behavior
The result of step 7 is same as step 4, 5.
Installed Versions
python = 3.10.12
pandas = 2.2.3
The text was updated successfully, but these errors were encountered: