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: DataFrame.eval fails with TypeError with multiline expr but works when eval line by line #59062

Open
3 tasks done
messense opened this issue Jun 21, 2024 · 0 comments · May be fixed by #59145
Open
3 tasks done

BUG: DataFrame.eval fails with TypeError with multiline expr but works when eval line by line #59062

messense opened this issue Jun 21, 2024 · 0 comments · May be fixed by #59145
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@messense
Copy link

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

df = pd.DataFrame({"first": [9.76, 9.76, 9.76], "last": [9.76, 9.76, 9.76], "pre": [9.75, 9.76, 9.76]})

expr = """first_ret = first / pre.fillna(first) - 1.0
last_ret = last / pre.fillna(first) - 1.0"""

# this fails with `TypeError: "value" parameter must be a scalar, dict or Series, but you passed a "ndarray"`
df.eval(expr)

# this works fine
for line in expr.splitlines():
    df.eval(line)

Issue Description

DataFrame.eval fails with TypeError: "value" parameter must be a scalar, dict or Series, but you passed a "ndarray" with multiline expr but works when eval line by line.

The culprit seems to be that

terms[i].update(terms[i].value.values)
modified Scope.resolvers.

Expected Behavior

df.eval(expr) also works fine

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.10.12.final.0
python-bits : 64
OS : Linux
OS-release : 6.5.0-1020-aws
Version : #20~22.04.1-Ubuntu SMP Wed May 1 16:10:50 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.2
numpy : 1.24.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.1.2
pip : 24.0
Cython : None
pytest : 8.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.9
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.6.0
gcsfs : None
matplotlib : 3.7.2
numba : 0.57.1
numexpr : 2.10.1
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 16.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2023.6.0
scipy : 1.12.0
sqlalchemy : 2.0.20
tables : None
tabulate : 0.9.0
xarray : 2023.8.0
xlrd : 2.0.1
zstandard : 0.22.0
tzdata : 2023.3
qtpy : None
pyqt5 : None

@messense messense added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 21, 2024
@Riyazul555 Riyazul555 linked a pull request Jun 29, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant