Skip to content

Multiline pd.eval() seems to be broken #31952

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

Open
resposit opened this issue Feb 13, 2020 · 5 comments
Open

Multiline pd.eval() seems to be broken #31952

resposit opened this issue Feb 13, 2020 · 5 comments
Labels
Bug expressions pd.eval, query

Comments

@resposit
Copy link

Code Sample:

In [93]: df
Out[93]:
   A  B
0  1  2
1  2  3
2  3  4
3  4  5

In [102]: pd.eval("""A = df.A - df.B
     ...: B = df.A + df.B
     ...: """,target=pd.DataFrame())

Problem description

....
/usr/local/lib64/python3.6/site-packages/pandas/core/computation/scope.py in resolve(self, key, is_local)
    201                 from pandas.core.computation.ops import UndefinedVariableError
    202
--> 203                 raise UndefinedVariableError(key, is_local)
    204
    205     def swapkey(self, old_key: str, new_key: str, new_value=None):

UndefinedVariableError: name 'df' is not defined

Expected Output

Out[106]:
   A  B
0 -1  1
1 -1  2
2 -1  3
3 -1  4

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1062.9.1.el7.x86_64
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.0
pip : 9.0.3
setuptools : 45.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.12.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@MarcoGorelli
Copy link
Member

MarcoGorelli commented Feb 13, 2020

What do you mean by "broken" - did the example you provided used to work in previous versions?

It looks like you're trying to pass multiple expressions to pandas.eval - only a single one is allowed, however (it doesn't seems like this is currently documented in https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.eval.html - are you interested in submitting a PR?)

Finally, even with a single line, I don't believe this example would work: [EDIT: with target=pd.DataFrame(), as in the OP's example, it does indeed work]

>>> pd.eval("A = df.A - df.B")
ValueError: cannot assign without a target object

Perhaps you meant

>>> df.eval("A = A-B")

? (even then, only a single expression is allowed) (EDIT: I wasn't quite right about this: only a single expression is allowed, but that expression may contain multiple lines, but only in DataFrame.eval and not in pandas.eval)

@resposit
Copy link
Author

Hi Marco,
I'm very new to pandas and don't know if this used to work in previous version.
Anyway, as far as I understood, you can actually pass multiple expressions (only assignements) to .eval() in a multiline string, as discussed here:
https://stackoverflow.com/questions/53779986/dynamic-expression-evaluation-in-pandas-using-pd-eval

@MarcoGorelli
Copy link
Member

MarcoGorelli commented Feb 13, 2020

Thanks @resposit - are you referring to the "Multiline Queries and Assignment" section?

If so, the command they give is a bit different (they're using DataFrame.eval rather than pd.eval, and they're prepending a @ symbol to the df variable) - the following still works:

>>> df.eval("""
... A = @df.A - @df.B
... B = @df.A + @df.B
... """
... )
   A  B
0 -1  3
1 -1  5
2 -1  7
3 -1  9

@resposit
Copy link
Author

Hi @MarcoGorelli
yes I was referring to that section. I know they're using DataFrame.eval().
In fact, using DataFrame.eval() you can also simplify your code like this:

In [108]: df.eval('''
     ...: A = A-B
     ...: B = A+B
     ...: ''')
Out[108]:
   A  B
0 -1  1
1 -1  2
2 -1  3
3 -1  4

I'm instead interested in using pd.eval() which, in principal, should also support multiline assignements.

@MarcoGorelli
Copy link
Member

I'm instead interested in using pd.eval() which, in principal, should also support multiline assignements.

OK, yes - if

df.eval(
    """
    A = A - B
    B = A + B
    """
)

works, there's no reason why

pd.eval(
    """
    A = df.A - df.B
    B = df.A + df.B
    """,
    target=pd.DataFrame(),
)

(which is what you originally wrote - apologies) shouldn't.

Looking at the source code, I don't know if the fact that it doesn't is intentional, pandas/core/computation/eval.py contains:

    if multi_line and target is None:
        raise ValueError(
            "multi-line expressions are only valid in the "
            "context of data, use DataFrame.eval"
        )

which suggests that, if target isn't None, then multi-line expressions are expected work. Will look into it

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

No branches or pull requests

2 participants