Skip to content

BUG: Fix bug in pd.eval when UnaryOp in function #48511

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 6 commits into from
Sep 14, 2022
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: 2 additions & 2 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ def visit_Call(self, node, side=None, **kwargs):

else:

new_args = [self.visit(arg).value for arg in node.args]
new_args = [self.visit(arg)(self.env) for arg in node.args]

for key in node.keywords:
if not isinstance(key, ast.keyword):
Expand All @@ -704,7 +704,7 @@ def visit_Call(self, node, side=None, **kwargs):
)

if key.arg:
kwargs[key.arg] = self.visit(key.value).value
kwargs[key.arg] = self.visit(key.value)(self.env)

name = self.env.add_tmp(res(*new_args, **kwargs))
return self.term_type(name=name, env=self.env)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,18 @@ def test_float_comparison_bin_op(self, dtype, expr):
res = df.eval(expr)
assert res.values == np.array([False])

def test_unary_in_function(self):
# GH 46471
df = DataFrame({"x": [0, 1, np.nan]})

result = df.eval("x.fillna(-1)")
expected = df.x.fillna(-1)
tm.assert_series_equal(result, expected, check_names=False)

result = df.eval("x.shift(1, fill_value=-1)")
expected = df.x.shift(1, fill_value=-1)
tm.assert_series_equal(result, expected, check_names=False)

@pytest.mark.parametrize(
"ex",
(
Expand Down