Skip to content

DOC: Error msg using Python keyword in numexpr query #18221 #18248

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

Merged
merged 1 commit into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ Categorical
Other
^^^^^

-
- Improved error message when attempting to use a Python keyword as an identifier in a numexpr query (:issue:`18221`)
-
-
9 changes: 8 additions & 1 deletion pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,14 @@ def __init__(self, env, engine, parser, preparser=_preparse):
def visit(self, node, **kwargs):
if isinstance(node, string_types):
clean = self.preparser(node)
node = ast.fix_missing_locations(ast.parse(clean))
try:
node = ast.fix_missing_locations(ast.parse(clean))
except SyntaxError as e:
from keyword import iskeyword
if any(iskeyword(x) for x in clean.split()):
e.msg = ("Python keyword not valid identifier"
" in numexpr query")
raise e

method = 'visit_' + node.__class__.__name__
visitor = getattr(self, method)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2267,7 +2267,8 @@ def query(self, expr, inplace=False, **kwargs):
by default, which allows you to treat both the index and columns of the
frame as a column in the frame.
The identifier ``index`` is used for the frame index; you can also
use the name of the index to identify it in a query.
use the name of the index to identify it in a query. Please note that
Python keywords may not be used as identifiers.

For further details and examples see the ``query`` documentation in
:ref:`indexing <indexing.query>`.
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,18 @@ def test_float_truncation(self):
expected = df.loc[[1], :]
tm.assert_frame_equal(expected, result)

def test_disallow_python_keywords(self):
# GH 18221
df = pd.DataFrame([[0, 0, 0]], columns=['foo', 'bar', 'class'])
msg = "Python keyword not valid identifier in numexpr query"
with tm.assert_raises_regex(SyntaxError, msg):
df.query('class == 0')

df = pd.DataFrame()
df.index.name = 'lambda'
with tm.assert_raises_regex(SyntaxError, msg):
df.query('lambda == 0')


class TestEvalNumexprPython(TestEvalNumexprPandas):

Expand Down