Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
df = pd.DataFrame((1,2,3), columns=['a#'])
df.query('a# > 2')
-------------------------------
KeyError Traceback (most recent call last)
File d:\Applications\Python\Python311\Lib\site-packages\pandas\core\computation\scope.py:231, in Scope.resolve(self, key, is_local)
230 if self.has_resolvers:
--> 231 return self.resolvers[key]
233 # if we're here that means that we have no locals and we also have
234 # no resolvers
File d:\Applications\Python\Python311\Lib\collections\__init__.py:1006, in ChainMap.__getitem__(self, key)
1005 pass
-> 1006 return self.__missing__(key)
File d:\Applications\Python\Python311\Lib\collections\__init__.py:998, in ChainMap.__missing__(self, key)
997 def __missing__(self, key):
--> 998 raise KeyError(key)
KeyError: 'a'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
File d:\Applications\Python\Python311\Lib\site-packages\pandas\core\computation\scope.py:242, in Scope.resolve(self, key, is_local)
238 try:
239 # last ditch effort we look in temporaries
240 # these are created when parsing indexing expressions
...
242 return self.temps[key]
243 except KeyError as err:
--> 244 raise UndefinedVariableError(key, is_local) from err
UndefinedVariableError: name 'a' is not defined
Issue Description
The query function seems to treat symbol # as a comment, it did not work as expected.
I also try to execute
df.query('`a#` > 2')
it still throws an exception
Traceback (most recent call last):
File d:\Applications\Python\Python311\Lib\site-packages\pandas\core\computation\parsing.py:192 in tokenize_string
yield tokenize_backtick_quoted_string(
File d:\Applications\Python\Python311\Lib\site-packages\pandas\core\computation\parsing.py:167 in tokenize_backtick_quoted_string
return BACKTICK_QUOTED_STRING, source[string_start:string_end]
UnboundLocalError: cannot access local variable 'string_end' where it is not associated with a value
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File d:\Applications\Python\Python311\Lib\site-packages\IPython\core\interactiveshell.py:3553 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
Cell In[59], [line 1](vscode-notebook-cell:?execution_count=59&line=1)
df.query('`a#` > 2')
File d:\Applications\Python\Python311\Lib\site-packages\pandas\core\frame.py:4823 in query
res = self.eval(expr, **kwargs)
File d:\Applications\Python\Python311\Lib\site-packages\pandas\core\frame.py:4949 in eval
...
raise SyntaxError(f"Failed to parse backticks in '{source}'.") from err
File <string>
SyntaxError: Failed to parse backticks in '`a#` > 2'.
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
The
query
function seems to treat symbol#
as a comment, it did not work as expected.I also try to execute
it still throws an exception
Expected Behavior
like df[df['a#'] > 2]
Installed Versions