Skip to content

ERR: Fix GH13139: better error message on invalid pd.eval and df.query input #14473

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ Bug Fixes

- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns``
is not scalar and ``values`` is not specified (:issue:`14380`)
- pd.eval('') and df.query('') now show better error message and raise ``ValueError`` (:issue: `13139`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more like

correctly raise ValueError on empty input to pd.eval() and df.query(). This

1 change: 1 addition & 0 deletions pandas/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def eval(expr, parser='pandas', engine=None, truediv=True,
"""
first_expr = True
if isinstance(expr, string_types):
_check_expression(expr)
exprs = [e for e in expr.splitlines() if e != '']
else:
exprs = [expr]
Expand Down
11 changes: 11 additions & 0 deletions pandas/computation/tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,17 @@ def test_bad_resolver_raises():
yield check_bad_resolver_raises, engine, parser


def check_empty_string_raises(engine, parser):
tm.skip_if_no_ne(engine)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue refernce in a comment

with tm.assertRaisesRegexp(ValueError, 'expr cannot be an empty string'):
pd.eval('', engine=engine, parser=parser)


def test_empty_string_raises():
for engine, parser in ENGINES_PARSERS:
yield check_empty_string_raises, engine, parser


def check_more_than_one_expression_raises(engine, parser):
tm.skip_if_no_ne(engine)
with tm.assertRaisesRegexp(SyntaxError,
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ def test_query_non_str(self):
with tm.assertRaisesRegexp(ValueError, msg):
df.query(111)

def test_query_empty_string(self):
# GH 13139
df = pd.DataFrame({'A': [1, 2, 3]})

msg = "expr cannot be an empty string"
with tm.assertRaisesRegexp(ValueError, msg):
df.query('')

def test_eval_resolvers_as_list(self):
# GH 14095
df = DataFrame(randn(10, 2), columns=list('ab'))
Expand Down