Skip to content

BUG: fix calling local references with keyword arguments in query #26426

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 8 commits into from
May 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 1 addition & 3 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,7 @@ def visit_Call(self, node, side=None, **kwargs):
"'{func}'".format(func=node.func.id))

if key.arg:
# TODO: bug?
kwargs.append(ast.keyword(
keyword.arg, self.visit(keyword.value))) # noqa
kwargs[key.arg] = self.visit(key.value).value

return self.const_type(res(*new_args, **kwargs), self.env)

Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,33 @@ def test_multi_line_expression_local_variable(self):
assert_frame_equal(expected, df)
assert ans is None

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

def local_func(a, b):
return b

expected = df.copy()
expected['c'] = expected['a'] * local_func(1, 7)
expected['d'] = expected['c'] + local_func(1, 7)
ans = df.eval("""
c = a * @local_func(1, 7)
d = c + @local_func(1, 7)
""", inplace=True)
assert_frame_equal(expected, df)
assert ans is None

expected = df.copy()
expected['c'] = expected['a'] * local_func(b=7, a=1)
expected['d'] = expected['c'] + local_func(b=7, a=1)
ans = df.eval("""
c = a * @local_func(b=7, a=1)
d = c + @local_func(b=7, a=1)
""", inplace=True)
assert_frame_equal(expected, df)
assert ans is None

Copy link
Member

Choose a reason for hiding this comment

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

Split this test into two.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gfyoung
done

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