Skip to content

df.eval does not see globals #16283

Closed
@maxnoe

Description

@maxnoe

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions