Skip to content

BUG: Multiline Eval broken for local variables after first line #15343

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
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,4 @@ Bug Fixes

- Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`)
- Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`)
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
5 changes: 2 additions & 3 deletions pandas/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,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 != '']
exprs = [e.strip() for e in expr.splitlines() if e.strip() != '']
else:
exprs = [expr]
multi_line = len(exprs) > 1
Expand All @@ -254,8 +254,7 @@ def eval(expr, parser='pandas', engine=None, truediv=True,
_check_for_locals(expr, level, parser)

# get our (possibly passed-in) scope
level += 1
env = _ensure_scope(level, global_dict=global_dict,
env = _ensure_scope(level + 1, global_dict=global_dict,
local_dict=local_dict, resolvers=resolvers,
target=target)

Expand Down
19 changes: 15 additions & 4 deletions pandas/computation/tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,6 @@ def test_assignment_fails(self):
local_dict={'df': df, 'df2': df2})

def test_assignment_column(self):
tm.skip_if_no_ne('numexpr')
df = DataFrame(np.random.randn(5, 2), columns=list('ab'))
orig_df = df.copy()

Expand Down Expand Up @@ -1346,7 +1345,6 @@ def test_column_in(self):

def assignment_not_inplace(self):
# GH 9297
tm.skip_if_no_ne('numexpr')
df = DataFrame(np.random.randn(5, 2), columns=list('ab'))

actual = df.eval('c = a + b', inplace=False)
Expand All @@ -1365,7 +1363,6 @@ def assignment_not_inplace(self):

def test_multi_line_expression(self):
# GH 11149
tm.skip_if_no_ne('numexpr')
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
expected = df.copy()

Expand Down Expand Up @@ -1393,7 +1390,6 @@ def test_multi_line_expression(self):

def test_multi_line_expression_not_inplace(self):
# GH 11149
tm.skip_if_no_ne('numexpr')
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
expected = df.copy()

Expand All @@ -1411,6 +1407,21 @@ def test_multi_line_expression_not_inplace(self):
e = a + 2""", inplace=False)
assert_frame_equal(expected, df)

def test_multi_line_expression_local_variable(self):
# GH 15342
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
expected = df.copy()

local_var = 7
expected['c'] = expected['a'] * local_var
expected['d'] = expected['c'] + local_var
ans = df.eval("""
c = a * @local_var
d = c + @local_var
Copy link
Contributor

Choose a reason for hiding this comment

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

add a test with a blank lines as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test does actually have a blank line. The """, inplace=... that terminates the string causes a line of nothing but space at the end of the string. Formulating the test case this way was how I found that the .strip() was needed in the compare.

""", inplace=True)
assert_frame_equal(expected, df)
self.assertIsNone(ans)

def test_assignment_in_query(self):
# GH 8664
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
Expand Down