{"record":{"id":"f87b8bd24aaaea14","repo":"pandas-dev/pandas","slug":"key","errorCode":null,"errorMessage":"{key}","messagePattern":"\\{key\\}","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/scope.py","lineNumber":52,"sourceCode":"    def __setitem__(self, key: _KT, value: _VT) -> None:\n        for mapping in self.maps:\n            if key in mapping:\n                mapping[key] = value\n                return\n        self.maps[0][key] = value\n\n    def __delitem__(self, key: _KT) -> None:\n        \"\"\"\n        Raises\n        ------\n        KeyError\n            If `key` doesn't exist.\n        \"\"\"\n        for mapping in self.maps:\n            if key in mapping:\n                del mapping[key]\n                return\n        raise KeyError(key)\n\n\ndef ensure_scope(\n    level: int, global_dict=None, local_dict=None, resolvers=(), target=None\n) -> Scope:\n    \"\"\"Ensure that we are grabbing the correct scope.\"\"\"\n    return Scope(\n        level + 1,\n        global_dict=global_dict,\n        local_dict=local_dict,\n        resolvers=resolvers,\n        target=target,\n    )\n\n\ndef _replacer(x) -> str:\n    \"\"\"\n    Replace a number with its hexadecimal representation. Used to tag","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/scope.py#L34-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ndel scope_chainmap['my_temp']  # KeyError if absent\n\n// after\nif 'my_temp' in scope_chainmap:\n    del scope_chainmap['my_temp']","handlingStrategy":"validation","validationCode":"def safe_delete(chainmap, key):\n    if key in chainmap:\n        del chainmap[key]\n        return True\n    return False","typeGuard":null,"tryCatchPattern":"try:\n    del scope_chainmap[key]\nexcept KeyError:\n    pass  # already absent; nothing to clean","preventionTips":["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."],"tags":["scope","eval","chainmap","keyerror","internal"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}