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 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ Bug Fixes
~~~~~~~~~



Categorical
^^^^^^^^^^^

Expand Down Expand Up @@ -380,6 +379,7 @@ Indexing
- Bug in which :meth:`DataFrame.append` produced an erroneous warning indicating that a ``KeyError`` will be thrown in the future when the data to be appended contains new columns (:issue:`22252`).
- Bug in which :meth:`DataFrame.to_csv` caused a segfault for a reindexed data frame, when the indices were single-level :class:`MultiIndex` (:issue:`26303`).
- Fixed bug where assigning a :class:`arrays.PandasArray` to a :class:`pandas.core.frame.DataFrame` would raise error (:issue:`26390`)
- Allow keyword arguments for callable local reference used in the :method:`DataFrame.query` string (:issue:`26426`)


Missing
Expand Down
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
34 changes: 34 additions & 0 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,40 @@ 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):
# 26426
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

def test_multi_line_expression_callable_local_variable_with_kwargs(self):
# 26426
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(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