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

df.eval does not see globals #16283

Closed
maxnoe opened this issue May 8, 2017 · 1 comment · Fixed by #58749
Closed

df.eval does not see globals #16283

maxnoe opened this issue May 8, 2017 · 1 comment · Fixed by #58749
Labels
Docs expressions pd.eval, query

Comments

@maxnoe
Copy link

maxnoe commented May 8, 2017

Code Sample, a copy-pastable example if possible

from numpy import pi
import pandas as pd

df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})

print('pi' in globals())
print(df.eval('a * b * pi'))

Problem description

This raises a

pandas.computation.ops.UndefinedVariableError: name 'pi' is not defined

According to the documentation of pd.eval which is used by pd.DataFrame.eval,
globals should be available:

local_dict : dict or None, optional
A dictionary of local variables, taken from locals() by default.
global_dict : dict or None, optional
A dictionary of global variables, taken from globals() by default.

The same exception is raised even if global_dict=globals() is passed to eval explicitly.

The only solution I found was to use:

pd.eval('a * b * pi', resolvers=[df, globals()])

Expected Output

No exception and the result of the calculation

Output of pd.show_versions()

# Paste the output here pd.show_versions() here

INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.8.0-51-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.19.2
nose: 1.3.7
pip: 9.0.1
setuptools: 35.0.1
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
statsmodels: 0.8.0
xarray: None
IPython: 6.0.0
sphinx: 1.4.8
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.2
matplotlib: 2.0.1
openpyxl: 2.4.1
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.5.3
html5lib: 0.999
httplib2: 0.9.2
apiclient: None
sqlalchemy: 1.1.9
pymysql: 0.7.2.None
psycopg2: None
jinja2: 2.9.6
boto: 2.43.0
pandas_datareader: Non

@chris-b1
Copy link
Contributor

chris-b1 commented May 8, 2017

Variables outside the df context should be prefixed with @ - see docs here - although it'd be good to add an example to the docstring too.

In [74]: df.eval('a * b * @pi')
Out[74]: 
0     9.424778
1    25.132741
dtype: float64

@chris-b1 chris-b1 added the Docs label May 8, 2017
@jbrockmendel jbrockmendel added the expressions pd.eval, query label Oct 22, 2019
mutricyl pushed a commit to mutricyl/pandas that referenced this issue May 17, 2024
mroeschke pushed a commit that referenced this issue May 31, 2024
* updating df.query and df.eval docstrings. resolves #16283

* typo

* adding 1 example

* changing wording following example added

* updating 'C C' to  'C&C' for eval

* updating 'C C' to 'C&C' for query

---------

Co-authored-by: Laurent Mutricy <laurent.mutricy@ekium.eu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs expressions pd.eval, query
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants