{"record":{"id":"db9d758f0515a7fb","repo":"pandas-dev/pandas","slug":"expr-cannot-be-an-empty-string","errorCode":null,"errorMessage":"expr cannot be an empty string","messagePattern":"expr cannot be an empty string","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/computation/eval.py","lineNumber":128,"sourceCode":"                )\n\n\ndef _check_expression(expr) -> None:\n    \"\"\"\n    Make sure an expression is not an empty string\n\n    Parameters\n    ----------\n    expr : object\n        An object that can be converted to a string\n\n    Raises\n    ------\n    ValueError\n      * If expr is an empty string\n    \"\"\"\n    if not expr:\n        raise ValueError(\"expr cannot be an empty string\")\n\n\ndef _convert_expression(expr) -> str:\n    \"\"\"\n    Convert an object to an expression.\n\n    This function converts an object to an expression (a unicode string) and\n    checks to make sure it isn't empty after conversion. This is used to\n    convert operators to their string representation for recursive calls to\n    :func:`~pandas.eval`.\n\n    Parameters\n    ----------\n    expr : object\n        The object to be converted to a string.\n\n    Returns\n    -------","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/computation/eval.py#L110-L146","documentation":"Raised by _check_expression() when the expr argument is falsy (empty string, None, empty bytes, 0, etc. — anything for which `not expr` is True). The check guards against passing an empty or whitespace-only meaningful expression, since evaluating nothing is meaningless. Note this checks truthiness, so a string of only spaces ('   ') is truthy and passes, but '' or None fails.","triggerScenarios":"Calling pd.eval('') or pd.eval(None); calling pd.eval(some_variable) where the variable was never assigned a real expression; programmatically building an expression string that ends up empty after stripping or conditional logic.","commonSituations":"Template/string-building code that concatenates optional clauses and produces '' when no clauses are selected; reading expressions from a config file or user input field that was left blank; passing df.query('') by accident.","solutions":["Guard the call: if expr.strip(): pd.eval(expr, ...) — skip evaluation for empty expressions.","Provide a default non-empty expression or branch to a no-op path when the expression is blank.","Validate user/config input before passing to eval: raise a clearer domain-specific error upstream."],"exampleFix":"// before\nexpr = build_filter(conditions)  # may return ''\npd.eval(expr)\n\n// after\nexpr = build_filter(conditions)\nif expr:\n    pd.eval(expr)","handlingStrategy":"validation","validationCode":"if not expr or (isinstance(expr, str) and not expr.strip()):\n    raise ValueError('expr must be a non-empty string')\npd.eval(expr)","typeGuard":"def is_nonempty_expr(e) -> bool:\n    return isinstance(e, str) and bool(e.strip())","tryCatchPattern":null,"preventionTips":["Validate expression strings at the boundary (config/UI) before eval.","Skip eval calls for empty conditions rather than passing ''.","Strip and assert non-empty in wrapper functions."],"tags":["pandas","eval","validation","empty-input","argument-error"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}