diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index b2facd4e2d0ec..70cd8fb18cf5f 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`) +- 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/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..ffa2cb0684b72 100644 --- a/pandas/computation/tests/test_eval.py +++ b/pandas/computation/tests/test_eval.py @@ -1891,6 +1891,18 @@ def test_bad_resolver_raises(): yield check_bad_resolver_raises, engine, parser +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) + + +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'))