pandas-dev/pandas · error · KeyError
{key}
Error message
{key} What it means
Raised by DeepChainMap.__delitem__ when the requested key is not present in any of the chained mappings. DeepChainMap backs the eval/query scope stack, so deleting a name that was never bound (or already removed) surfaces as a bare KeyError whose message is just the key.
Source
Thrown at pandas/core/computation/scope.py:52
def __setitem__(self, key: _KT, value: _VT) -> None:
for mapping in self.maps:
if key in mapping:
mapping[key] = value
return
self.maps[0][key] = value
def __delitem__(self, key: _KT) -> None:
"""
Raises
------
KeyError
If `key` doesn't exist.
"""
for mapping in self.maps:
if key in mapping:
del mapping[key]
return
raise KeyError(key)
def ensure_scope(
level: int, global_dict=None, local_dict=None, resolvers=(), target=None
) -> Scope:
"""Ensure that we are grabbing the correct scope."""
return Scope(
level + 1,
global_dict=global_dict,
local_dict=local_dict,
resolvers=resolvers,
target=target,
)
def _replacer(x) -> str:
"""
Replace a number with its hexadecimal representation. Used to tagView on GitHub (pinned to 3b7651241d)
Solutions
- Guard deletions with a membership check: if key in dcm: del dcm[key].
- Use dcm.maps iteration to confirm the key lives in a specific mapping before deleting.
- Avoid mutating the internal DeepChainMap of eval/query scopes directly; let pandas manage scope lifetime.
Example fix
// before
del scope_chainmap['my_temp'] # KeyError if absent
// after
if 'my_temp' in scope_chainmap:
del scope_chainmap['my_temp'] Defensive patterns
Strategy: validation
Validate before calling
def safe_delete(chainmap, key):
if key in chainmap:
del chainmap[key]
return True
return False Try / catch
try:
del scope_chainmap[key]
except KeyError:
pass # already absent; nothing to clean Prevention
- Always check membership before deleting from a DeepChainMap.
- Let pandas own the lifetime of eval/query scopes; do not delete internals manually.
- Track inserted temporaries in your own set so deletions are idempotent.
When it happens
Trigger: Internal scope cleanup calling del scope[key] for a temporary/resolver key that was never inserted or was already deleted; user code reaching into the DeepChainMap returned by an eval scope and deleting a missing key; double-deletion of the same temporary variable.
Common situations: Rare from the public API; can appear in custom resolvers or when manipulating the scope object handed to eval. Most often a secondary symptom of logic that assumes a name exists in scope when it doesn't.
Related errors
- The '@' prefix is not allowed in top-level eval calls. pleas
- name '{name}' is not defined
- invalid validation method '{method}'
- Label(s) {list(cols)} do not exist
- No accumulation for {func} implemented on BaseMaskedArray
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/f87b8bd24aaaea14.
Report an issue: GitHub.