From 9a5c55fa8d66e342f04dd57e00116d89dcd4a51a Mon Sep 17 00:00:00 2001 From: Thiago Serafim Date: Fri, 21 Oct 2016 23:07:35 -0200 Subject: [PATCH 1/2] Fix GH13139: better error message on invalid pd.eval and df.query input --- doc/source/whatsnew/v0.19.1.txt | 1 + pandas/computation/eval.py | 1 + pandas/computation/tests/test_eval.py | 11 +++++++++++ pandas/tests/frame/test_query_eval.py | 8 ++++++++ 4 files changed, 21 insertions(+) diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index b2facd4e2d0ec..1adca76702411 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -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`) \ No newline at end of file diff --git a/pandas/computation/eval.py b/pandas/computation/eval.py index 6c5c631a6bf0e..fffde4d9db867 100644 --- a/pandas/computation/eval.py +++ b/pandas/computation/eval.py @@ -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] diff --git a/pandas/computation/tests/test_eval.py b/pandas/computation/tests/test_eval.py index f480eae2dd04d..19e0473535fd7 100644 --- a/pandas/computation/tests/test_eval.py +++ b/pandas/computation/tests/test_eval.py @@ -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) + 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, diff --git a/pandas/tests/frame/test_query_eval.py b/pandas/tests/frame/test_query_eval.py index 85159de64d83e..29662c5addb75 100644 --- a/pandas/tests/frame/test_query_eval.py +++ b/pandas/tests/frame/test_query_eval.py @@ -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')) From 77483dd1efb42e1dbe1b997994e49a6b0ff1a8f4 Mon Sep 17 00:00:00 2001 From: Thiago Serafim Date: Mon, 24 Oct 2016 19:10:51 -0200 Subject: [PATCH 2/2] ERR: correctly raise ValueError on empty input to pd.eval() and df.query() (#13139) --- doc/source/whatsnew/v0.19.1.txt | 2 +- pandas/computation/tests/test_eval.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index 1adca76702411..70cd8fb18cf5f 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -57,4 +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`) \ No newline at end of file +- corrrecly raise ``ValueError`` on empty input to pd.eval() and df.query() (:issue: `13139`) \ No newline at end of file diff --git a/pandas/computation/tests/test_eval.py b/pandas/computation/tests/test_eval.py index 19e0473535fd7..ffa2cb0684b72 100644 --- a/pandas/computation/tests/test_eval.py +++ b/pandas/computation/tests/test_eval.py @@ -1892,6 +1892,7 @@ def test_bad_resolver_raises(): def check_empty_string_raises(engine, parser): + # GH 13139 tm.skip_if_no_ne(engine) with tm.assertRaisesRegexp(ValueError, 'expr cannot be an empty string'): pd.eval('', engine=engine, parser=parser)