Skip to content
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

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

Merged
merged 8 commits into from May 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Expand Up @@ -295,7 +295,6 @@ Bug Fixes
~~~~~~~~~



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

Expand Down Expand Up @@ -475,6 +474,7 @@ Other
- Removed unused C functions from vendored UltraJSON implementation (:issue:`26198`)
- Bug in :func:`factorize` when passing an ``ExtensionArray`` with a custom ``na_sentinel`` (:issue:`25696`).
- Allow :class:`Index` and :class:`RangeIndex` to be passed to numpy ``min`` and ``max`` functions.
- Allow keyword arguments for callable local reference used in the ``DataFrame.query`` string
jreback marked this conversation as resolved.
Show resolved Hide resolved


.. _whatsnew_0.250.contributors:
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/computation/expr.py
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
32 changes: 32 additions & 0 deletions pandas/tests/computation/test_eval.py
Expand Up @@ -1345,6 +1345,38 @@ 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):
jreback marked this conversation as resolved.
Show resolved Hide resolved
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):
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
jreback marked this conversation as resolved.
Show resolved Hide resolved

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