pola-rs/polars · error · TypeError
invalid predicate for `filter
Error message
invalid predicate for `filter`: {err} What it means
LazyFrame._filter() coerces each predicate into an expression; when coercion fails it raises this TypeError showing the offending predicate (repr, or a Series repr with dtype). Valid predicates are boolean pl.Expr, booleans, parseable strings, or boolean Series of matching length; kwargs become equality constraints.
Solutions
- Wrap logic in expressions: `lf.filter(pl.col('a') > 1)`
- Convert a boolean Series explicitly: `lf.filter(pl.lit(series))` with matching length
- Replace string/list/ndarray predicates with expressions
- For dynamic values use `pl.lit(value)` inside the expression
Example fix
// before df = lf.filter([True, False, True]) // after df = lf.filter(pl.Series([True, False, True])) # boolean Series of matching length
Defensive patterns
Strategy: type-guard
Validate before calling
def is_valid_predicate(p) -> bool:
return isinstance(p, (pl.Expr, bool, str)) or (
isinstance(p, pl.Series) and p.dtype == pl.Boolean
) Type guard
def is_expr(p) -> bool:
return isinstance(p, pl.Expr) Try / catch
try:
df = lf.filter(pred)
except TypeError as e:
if "invalid predicate for `filter`" in str(e):
df = lf.filter(pl.lit(pred))
else:
raise Prevention
- Always filter with pl.Expr (pl.col/pl.lit based) expressions
- Ensure Series predicates are Boolean dtype and matching length
- Never pass Python lists, numpy masks, or pandas Series directly
When it happens
Trigger: `lf.filter(pl.col('a'))` on a non-boolean column; `lf.filter([True, False])` (plain list); a Series predicate with wrong dtype/length; passing pandas masks or arbitrary Python objects.
Common situations: Passing pandas boolean masks instead of polars expressions; filtering by a numeric column expecting pandas-like truthiness; typos yielding non-expression objects.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- arr.to_struct() got a str instead of a list. hint: pass
- at least one predicate or constraint must be provided
- cannot call `map_groups` when filtering groups with `having`
- cannot call `map_groups` when grouping by an expression
- cannot call `map_groups` when grouping by named expressions
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/a1b3be8a3b272ca8.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/lazyframe/frame.py:4451
# note: identify masks separately from predicates
if is_bool_sequence(p, include_series=True):
boolean_masks.append(pl.Series(p, dtype=Boolean))
elif (
(is_seq := is_sequence(p))
and any(not isinstance(x, pl.Expr) for x in p)
) or (
not is_seq
and not isinstance(p, pl.Expr)
and not (isinstance(p, str) and p in self.collect_schema())
):
err = (
f"Series(…, dtype={p.dtype})"
if isinstance(p, pl.Series)
else repr(p)
)
msg = f"invalid predicate for `filter`: {err}"
raise TypeError(msg)
else:
all_predicates.extend(
wrap_expr(x) for x in parse_into_list_of_expressions(p)
)
# unpack equality constraints from kwargs
all_predicates.extend(
F.col(name).eq(value) for name, value in constraints.items()
)
if not (all_predicates or boolean_masks):
msg = "at least one predicate or constraint must be provided"
raise TypeError(msg)
# if multiple predicates, combine as 'horizontal' expression
combined_predicate = (
(
F.all_horizontal(*all_predicates)
if len(all_predicates) > 1View on GitHub (pinned to fe841f959e)