Skip to content

BUG: Fix AttributeError in pd.eval for method calls on binary operations #61198

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 1 commit into from
Mar 31, 2025
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ Other
- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
- Bug in :class:`Series` ignoring errors when trying to convert :class:`Series` input data to the given ``dtype`` (:issue:`60728`)
- Bug in :func:`eval` on :class:`ExtensionArray` on including division ``/`` failed with a ``TypeError``. (:issue:`58748`)
- Bug in :func:`eval` where method calls on binary operations like ``(x + y).dropna()`` would raise ``AttributeError: 'BinOp' object has no attribute 'value'`` (:issue:`61175`)
- Bug in :func:`eval` where the names of the :class:`Series` were not preserved when using ``engine="numexpr"``. (:issue:`10239`)
- Bug in :func:`eval` with ``engine="numexpr"`` returning unexpected result for float division. (:issue:`59736`)
- Bug in :func:`to_numeric` raising ``TypeError`` when ``arg`` is a :class:`Timedelta` or :class:`Timestamp` scalar. (:issue:`59944`)
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,11 @@ def visit_Attribute(self, node, **kwargs):
ctx = node.ctx
if isinstance(ctx, ast.Load):
# resolve the value
resolved = self.visit(value).value
visited_value = self.visit(value)
if hasattr(visited_value, "value"):
resolved = visited_value.value
else:
resolved = visited_value(self.env)
try:
v = getattr(resolved, attr)
name = self.env.add_tmp(v)
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2006,3 +2006,24 @@ def test_eval_float_div_numexpr():
result = pd.eval("1 / 2", engine="numexpr")
expected = 0.5
assert result == expected


def test_method_calls_on_binop():
# GH 61175
x = Series([1, 2, 3, 5])
y = Series([2, 3, 4])

# Method call on binary operation result
result = pd.eval("(x + y).dropna()")
expected = (x + y).dropna()
tm.assert_series_equal(result, expected)

# Test with other binary operations
result = pd.eval("(x * y).dropna()")
expected = (x * y).dropna()
tm.assert_series_equal(result, expected)

# Test with method chaining
result = pd.eval("(x + y).dropna().reset_index(drop=True)")
expected = (x + y).dropna().reset_index(drop=True)
tm.assert_series_equal(result, expected)
Loading