pola-rs/polars · error · TypeError
at least one predicate or constraint must be provided
Error message
at least one predicate or constraint must be provided
What it means
_combine_predicates (parse/expr.py:304) requires at least one predicate across the *predicates and **constraints accepted by pl.when(...), Expr.when(...), and Expr.filter(...). With zero inputs there is nothing to AND together, so it raises this TypeError rather than producing a meaningless expression that would fail later.
Source
Thrown at py-polars/src/polars/_utils/parse/expr.py:304
"""
all_predicates = _parse_positional_inputs(predicates) # type: ignore[arg-type]
if constraints:
constraint_predicates = _parse_constraints(constraints)
all_predicates.extend(constraint_predicates)
return _combine_predicates(all_predicates)
def _parse_constraints(constraints: dict[str, IntoExpr]) -> Iterable[PyExpr]:
for name, value in constraints.items():
yield F.col(name).eq(value)._pyexpr
def _combine_predicates(predicates: list[PyExpr]) -> PyExpr:
if not predicates:
msg = "at least one predicate or constraint must be provided"
raise TypeError(msg)
if len(predicates) == 1:
return predicates[0]
return plr.all_horizontal(predicates)
View on GitHub (pinned to df599052da)
Solutions
- Default to a neutral predicate: preds = predicates or [pl.lit(True)] before pl.when(*preds)
- Skip the when/otherwise chain entirely when no predicates apply
- Validate at the API boundary: require at least one predicate or constraint
Example fix
# before pl.when(*predicates).then(x).otherwise(y) # predicates == [] # after predicates = predicates or [pl.lit(True)] pl.when(*predicates).then(x).otherwise(y)
Defensive patterns
Strategy: validation
Validate before calling
if not predicates and not constraints:
raise TypeError("at least one predicate or constraint must be provided")
pl.when(*predicates, **constraints) Type guard
def has_any_predicate(predicates, constraints) -> bool:
return len(predicates) > 0 or len(constraints) > 0 Prevention
- Default empty predicate lists to [pl.lit(True)] when a when-chain is still required
- Validate user-supplied filter lists before forwarding to when/filter
- Consider skipping the when/otherwise branch entirely when nothing applies
When it happens
Trigger: pl.when() called with no arguments; dynamically assembled predicate lists that end up empty: pl.when(*preds) with preds == []; expr.filter() with no predicates; when(**constraints) with an empty dict.
Common situations: Config/user-driven predicate builders; helper functions forwarding *args into when()/filter(); branches where all optional filters are absent.
Related errors
- selecting rows by passing a boolean mask to `__getitem__` is
- invalid predicate for `filter`: {err}
- cannot select columns using key of type {qualified_type_name
- cannot select rows using key of type {qualified_type_name(ke
- cannot treat Series of type {s.dtype} as indices
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/6214ccee128fdfad.
Report an issue: GitHub.