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: AttributeError on method call after binary operation in eval expression #61175

Closed
2 of 3 tasks
stewit opened this issue Mar 25, 2025 · 5 comments · Fixed by #61198
Closed
2 of 3 tasks

BUG: AttributeError on method call after binary operation in eval expression #61175

stewit opened this issue Mar 25, 2025 · 5 comments · Fixed by #61198
Assignees
Labels
Bug expressions pd.eval, query
Milestone

Comments

@stewit
Copy link

stewit commented Mar 25, 2025

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

x = pd.Series([1,2,3,5])
y = pd.Series([2,3,4])

pd.eval("(x + y).dropna()")
# raises AttributeError: 'BinOp' object has no attribute 'value'

# Note that something like pd.eval("(x.dropna() + y)") works!

Issue Description

Also effects other Series methods called on the result of a binary operation, which work well if applied to one operand.

See also #24670

Expected Behavior

Should return a Series of sums with nan values removed. Generally I would expect that it should be possible to apply a Series method to the result of a binary operation as part of an eval expression.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.13.2
python-bits : 64
OS : Linux
OS-release : 6.13.7-arch1-1
Version : #1 SMP PREEMPT_DYNAMIC Thu, 13 Mar 2025 18:12:00 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8

pandas : 2.2.3
numpy : 2.2.3
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : 9.0.1
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2025.2.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.5
lxml.etree : 5.3.1
matplotlib : 3.10.1
numba : None
numexpr : 2.10.2
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.5
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.2
sqlalchemy : 2.0.38
tables : 3.10.2
tabulate : None
xarray : 2025.1.2
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.1
qtpy : None
pyqt5 : None

@stewit stewit added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 25, 2025
@narukaze132
Copy link

Thanks for reporting the issue. For now, depending on your use case, I would recommend moving the method call to outside the eval expression if possible. This works fine in the example you outlined, and would likely perform better in most circumstances. (If you need to choose the method dynamically, getattr() can handle that.)

Alternatively, you could use the default eval() function in Python, which also works with the given example, but I'd avoid it if possible due to performance and security implications.

@snitish
Copy link
Member

snitish commented Mar 25, 2025

Confirmed on main. PRs to fix are welcome.

@snitish snitish added expressions pd.eval, query and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 25, 2025
@ycdjun
Copy link

ycdjun commented Mar 26, 2025

Take

@ycdjun
Copy link

ycdjun commented Mar 26, 2025

Unassign

@myenugula
Copy link
Contributor

Take

@rhshadrach rhshadrach added this to the 3.0 milestone Mar 29, 2025
mroeschke pushed a commit that referenced this issue Mar 31, 2025
…ons (#61198)

BUG: Fix AttributeError in pd.eval for method calls on binary operations (#61175)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug expressions pd.eval, query
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants