{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/computation/eval.py#L110-L146","documentation":"Raised by _check_expression (pandas/core/computation/eval.py:128) as a ValueError when the expression string passed to pd.eval / DataFrame.eval / DataFrame.query is empty (or falsy after string conversion). An empty expression has no meaning and cannot be evaluated, so it is rejected before parsing is attempted.","triggerScenarios":"`pd.eval('')`, `df.query('')`, `df.eval('')`, or an expression that becomes empty after string conversion (e.g. passing None or whitespace-only in some paths, or a programmatically-built expression that collapsed to '').","commonSituations":"Building expressions dynamically from empty filters/inputs, stripping user input down to '', or passing a falsy variable unintentionally.","solutions":["Guard dynamic expressions: `if expr_str.strip(): df.query(expr_str)` else skip.","Default to a tautology when filtering: use `df.query('index >= 0')` or skip the query when no filter is needed.","Validate user-provided query strings before passing them to eval/query."],"exampleFix":"# before\nexpr = build_filter(filters)  # may be ''\ndf.query(expr)\n\n# after\nexpr = build_filter(filters)\nif expr.strip():\n    df = df.query(expr)","handlingStrategy":"validation","validationCode":"def safe_query(df, expr):\n    if not expr or not expr.strip():\n        return df\n    return df.query(expr)","typeGuard":"def is_nonempty_expr(expr) -> bool:\n    return isinstance(expr, str) and bool(expr.strip())","tryCatchPattern":"try:\n    result = df.query(expr)\nexcept ValueError as e:\n    if 'empty string' in str(e):\n        result = df  # no-op for empty filter\n    else:\n        raise","preventionTips":["Strip and check query/eval strings before passing: if expr.strip(): ....","Default dynamic filters to a tautology or skip the call.","Validate user-provided expressions for emptiness upstream."],"tags":["eval","query","empty-string","validation","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}