Skip to content

BUG: Fix bug when using df.query with "str.contains()" #25813

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
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -322,7 +322,7 @@ Sparse
Other
^^^^^

-
- Bug in :class:`BaseExprVisitor` for using named kwargs in :func:`eval` expressions.
-
-

Expand Down
5 changes: 1 addition & 4 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,8 @@ def visit_Call_35(self, node, side=None, **kwargs):
if not isinstance(key, ast.keyword):
raise ValueError("keyword error in function call "
"'{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)()

return self.const_type(res(*new_args, **kwargs), 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 @@ -1903,3 +1903,15 @@ def test_validate_bool_args(self):
for value in invalid_values:
with pytest.raises(ValueError):
pd.eval("2+2", inplace=value)


def test_query_str_contains():
df = pd.DataFrame([["I", "XYZ"], ["IJ", None]], columns=['A', 'B'])

expected = df[df["A"].str.contains("J")]
result = df.query("A.str.contains('J')", engine="python")
tm.assert_frame_equal(result, expected)

expected = df[df["B"].str.contains("Z", na=False)]
result = df.query("B.str.contains('Z', na=False)", engine="python")
tm.assert_frame_equal(result, expected)