pandas-dev/pandas · error · ValueError
expr cannot be an empty string
Error message
expr cannot be an empty string
What it means
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.
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.
Example fix
// before
expr = build_filter(conditions) # may return ''
pd.eval(expr)
// after
expr = build_filter(conditions)
if expr:
pd.eval(expr) Defensive patterns
Strategy: validation
Validate before calling
if not expr or (isinstance(expr, str) and not expr.strip()):
raise ValueError('expr must be a non-empty string')
pd.eval(expr) Type guard
def is_nonempty_expr(e) -> bool:
return isinstance(e, str) and bool(e.strip()) Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid parser ' ' passed, valid parsers are
- cannot assign without a target object
- Cannot operate inplace if there is no assignment
- expr must be a string to be evaluated
- Multi-line expressions are only valid if all expressions…
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/db9d758f0515a7fb.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/computation/eval.py:128
)
def _check_expression(expr) -> None:
"""
Make sure an expression is not an empty string
Parameters
----------
expr : object
An object that can be converted to a string
Raises
------
ValueError
* If expr is an empty string
"""
if not expr:
raise ValueError("expr cannot be an empty string")
def _convert_expression(expr) -> str:
"""
Convert an object to an expression.
This function converts an object to an expression (a unicode string) and
checks to make sure it isn't empty after conversion. This is used to
convert operators to their string representation for recursive calls to
:func:`~pandas.eval`.
Parameters
----------
expr : object
The object to be converted to a string.
Returns
-------View on GitHub (pinned to 3b7651241d)