Skip to content
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

Cannot parse query for df.query() with column name containing parenthesis. #28576

Closed
ash-ishh opened this issue Sep 23, 2019 · 5 comments
Closed

Comments

@ash-ishh
Copy link

Code:
 import pandas as pd

 a = {'abc':[1,2,3],'xy (z)':[4,5,6]}

 df = pd.DataFrame(a)
 query = "`xy (z)` == 2"
 new_df = df.query(query)

Error :

xy_(z)_BACKTICK_QUOTED_STRING ==2
SyntaxError: invalid syntax

Problem description

df.query is not able to parse the query where column name of df is not valid python identifier.

Workaround: https://stackoverflow.com/questions/40045545/pandas-query-string-where-column-name-contains-special-characters

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.187-1.el7.elrepo.x86_64
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.1
numpy : 1.17.0
pytz : 2019.2
dateutil : 2.8.0
pip : 18.1
setuptools : 40.6.2
Cython : 0.28.2
pytest : 5.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.0.4
lxml.etree : 4.4.1
html5lib : 1.0.1
pymysql : 0.9.3
psycopg2 : 2.8.3 (dt dec pq3 ext lo64)
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.8.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 2.6.2
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.1.0
sqlalchemy : 1.3.3
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : 1.0.4

@dhwanils95
Copy link

Yess, I am facing the exact same issue. The column name has paranthesis character which it is not able to parse and is raising exception.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Sep 23, 2019 via email

@ash-ishh
Copy link
Author

I tried above commit but the case doesn't seem to work.
Also the functionality mentioned in PR (digit, start with a number, or are separated by operators) is not working.

I installed pandas from this commit :
commit 179cd3b
Merge: 08b6400 23e7595
Author: Hielke Walinga hielkewalinga@gmail.com
Date: Thu Sep 12 16:10:02 2019 +0200
Merge branch 'allow-special-characters-query' of https://github.com/hwalinga/pandas into allow-special-characters-query

Pandas version: 0.24.0.dev0+2984.g179cd3b

@hwalinga
Copy link
Contributor

Hello, thanks for the issue.

This is indeed an edge case. I have this working now. Check the latest commits.

@ash-ishh
Copy link
Author

Thank you @hwalinga it is working now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants