Skip to content

BUG: Eval scopes ignoring empty dictionaries (#47084) #47085

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

Merged
merged 16 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions doc/source/whatsnew/v1.4.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
-

Conversion
^^^^^^^^^^
- Bug in :meth:`pd.eval`, :meth:`DataFrame.eval` and :meth:`DataFrame.query` where passing empty ``local_dict`` or ``global_dict`` was treated as passing ``None`` (:issue:`47084`)

.. ---------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import ast
from functools import partial
from typing import Any
from typing import Any, Optional

import numpy as np

Expand Down Expand Up @@ -563,7 +563,7 @@ def __init__(
self._visitor = None

# capture the environment if needed
local_dict: DeepChainMap[Any, Any] = DeepChainMap()
local_dict: Optional[DeepChainMap[Any, Any]] = None

if isinstance(where, PyTablesExpr):
local_dict = where.env.scope
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/computation/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ def __init__(
# shallow copy here because we don't want to replace what's in
# scope when we align terms (alignment accesses the underlying
# numpy array of pandas objects)
scope_global = self.scope.new_child((global_dict or frame.f_globals).copy())
scope_global = self.scope.new_child(
(global_dict if global_dict is not None else frame.f_globals).copy()
)
self.scope = DeepChainMap(scope_global)
if not isinstance(local_dict, Scope):
scope_local = self.scope.new_child(
(local_dict or frame.f_locals).copy()
(local_dict if local_dict is not None else frame.f_locals).copy()
)
self.scope = DeepChainMap(scope_local)
finally:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from pandas.core.computation.ops import (
ARITH_OPS_SYMS,
SPECIAL_CASE_ARITH_OPS_SYMS,
UndefinedVariableError,
_binary_math_ops,
_binary_ops_dict,
_unary_math_ops,
Expand Down Expand Up @@ -1659,6 +1660,20 @@ def test_no_new_globals(self, engine, parser):
gbls2 = globals().copy()
assert gbls == gbls2

def test_empty_locals(self, engine, parser):
# GH 47084
x = 1 # noqa: F841
msg = "name 'x' is not defined"
with pytest.raises(UndefinedVariableError, match=msg):
pd.eval("x + 1", engine=engine, parser=parser, local_dict={})

def test_empty_globals(self, engine, parser):
# GH 47084
msg = "name '_var_s' is not defined"
e = "_var_s * 2"
with pytest.raises(UndefinedVariableError, match=msg):
pd.eval(e, engine=engine, parser=parser, global_dict={})


@td.skip_if_no_ne
def test_invalid_engine():
Expand Down