Filter accepts syntax error and returns wrong results without warning.

This issue has been tracked since 2022-09-22.

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

import pandas as pd

data = {"ID": [10, 20, 30, 40, 50, 60, 70], "Age": [12, 14, 13, 12, 14, 13, 15], "NAME": ['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG']}
df1 = pd.DataFrame(data)

# It is ok
print(df1.query('Age >= 12').shape)  # (7, 3)
print(df1[(df1.Age >= 12)].shape)  # (7, 3)
print(df1[df1.Age >= 12].shape)  # (7, 3)

# It is ok also
print(df1.query('Age >= 12 & ID > 50').shape) # (2, 3)
print(df1[(df1.Age >= 12) & (df1.ID > 50)].shape) # (2, 3)

# It is wrong, but with no warning gets wrong results
print(df1[(df1.Age >= 12) & df1.ID > 50].shape)  # (0, 3)
print(df1[df1.Age >= 12 & (df1.ID > 50)].shape)  # (7, 3)

Issue Description

The two syntaxes below are wrong, missing the parentheses. As a result, one of the conditions will not be executed.
df1[(df1.Age >= 12) & df1.ID > 50]
df1[df1.Age >= 12 & (df1.ID > 50)]

However, the library returns wrong values ​​without any kind of warning.

Expected Behavior

The command with the wrong syntax (missing the parentheses) should not be executed, it should return an error or warning.

Installed Versions

INSTALLED VERSIONS

commit : 66e3805
python : 3.7.14.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.133+
Version : #1 SMP Fri Aug 26 08:44:51 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.5
numpy : 1.21.6
pytz : 2022.2.1
dateutil : 2.8.2
pip : 21.1.3
setuptools : 57.4.0
Cython : 0.29.32
pytest : 3.6.4
hypothesis : None
sphinx : 1.8.6
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.9.3 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.9.0
pandas_datareader: 0.9.0
bs4 : 4.6.3
bottleneck : None
fsspec : 2022.8.2
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.10
pandas_gbq : 0.13.3
pyarrow : 6.0.1
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : 1.4.41
tables : 3.7.0
tabulate : 0.8.10
xarray : 0.20.2
xlrd : 1.1.0
xlwt : 1.3.0
numba : 0.56.2

MarcoGorelli wrote this answer on 2022-09-22

Thanks for the report, but this is valid Python syntax - I don't think pandas can throw a warning or error here because it doesn't know what you were intending

closing for now then

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-10-04
Star Count 35430
Watcher Count 1120
Fork Count 15089
Issue Count 3589

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date