Description
Code Sample
>>> import pandas as pd
>>> d = pd.DataFrame(([1, 2], [3, 4]), columns=["Col 1", "Col 2"])
>>> d.query("`Col 1` < `Col 2`") # This works as expected.
Col 1 Col 2
0 1 2
1 3 4
>>> d.query("`Col 1` < `Col 2`") # 2 whitespaces between "Col" and "1". Also works, but not expected?
Col 1 Col 2
0 1 2
1 3 4
>>> d = pd.DataFrame(([1, 2], [3, 4]), columns=["Col 1", "Col 2"]) # 2 whitespaces between "Col" and "1" in the column name.
>>> d.query("`Col 1` < `Col 2`") # 2 whitespaces between "Col" and "1" in the query.
pandas.core.computation.ops.UndefinedVariableError: name 'Col_1_BACKTICK_QUOTED_STRING' is not defined
>>> d.query("`Col 1` < `Col 2`") # 10 whitespaces between "Col" and "1".
pandas.core.computation.ops.UndefinedVariableError: name 'Col_1_BACKTICK_QUOTED_STRING' is not defined
>>> d.eval("`Col 1` < `Col 2`") # 2 whitespaces between "Col" and "1".
pandas.core.computation.ops.UndefinedVariableError: name 'Col_1_BACKTICK_QUOTED_STRING' is not defined
Problem description
When using backticks in the input to DataFrame.query
or DataFrame.eval
, it seems that the number of whitespaces between Col
and 1
, is mapped into: Col_1_BACKTICK_QUOTED_STRING
, regardless of how many whitespaces there are between Col
and 1
.
Having 2 whitespaces when defining the column name in the DataFrame, it seems that the column name is defined as: Col__1_BACKTICK_QUOTED_STRING
.
With 10 whitespaces: Col__________1_BACKTICK_QUOTED_STRING
, and so forth.
This makes the number of whitespaces in the column name important, as column name will be based on the amount of them. However, the number of whitespaces in the input to DataFrame.query
or DataFrame.eval
does not seem to have any affect, and will always have one underscore, and not match the column name if it has more than one whitespace.
Expected Output
The expected output would be to have the column name and the input to query
or eval
transformed with the same function, having a one to one mapping between whitespaces and underscores when definding the variable. i.e.
2 whitespaces: Col__1_BACKTICK_QUOTED_STRING
.
10 whitespaces: Col__________1_BACKTICK_QUOTED_STRING
.
Output of pd.show_versions()
pandas : 0.25.1
numpy : 1.17.2
pytz : 2019.3
dateutil : 2.8.0
pip : 19.2.3
setuptools : 41.4.0
Cython : None
pytest : 5.2.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.8.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : 2.7.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.15.0
pytables : None
s3fs : None
scipy : 1.3.1
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None