Skip to content

BUG: Using datetime global in query expression raises UndefinedVariableError #50442

Open
@phillipuniverse

Description

@phillipuniverse

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

def test_reproduction():
    unfiltered = DataFrame(
        {
            "planned_date": {
                "0": date.fromisoformat("2020-10-01"),
                "1": date.fromisoformat("2020-08-01"),
            },
        }
    )
    filtered = unfiltered.query(f'planned_date >= datetime.strptime("2020-09-01", "%Y-%m-%d").date()')\
        .copy().reset_index(drop=True)
    assert (
        filtered.to_json()
        == unfiltered.iloc[[0]].reset_index(drop=True).to_json()
    )

This results in the following exception, whereas on versions before 1.5.0 it works

            except KeyError as err:
>               raise UndefinedVariableError(key, is_local) from err
E               pandas.errors.UndefinedVariableError: name 'datetime' is not defined

Issue Description

The behavior change in #47273 appears to make it to where I cannot use the datetime global in a query string being passed to eval. I'm also just not sure what the right way is to now access globals within an eval, is there a better way to write this query?

Expected Behavior

Test passes

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.9.16.final.0
python-bits : 64
OS : Darwin
OS-release : 22.1.0
Version : Darwin Kernel Version 22.1.0: Sun Oct 9 20:14:54 PDT 2022; root:xnu-8792.41.9~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.1
numpy : 1.23.0
pytz : 2022.1
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.1.2
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.0
html5lib : None
pymysql : 1.0.2
psycopg2 : 2.9.3
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.5.4
snappy : None
sqlalchemy : 1.4.39
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

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