{"record":{"id":"a1b3be8a3b272ca8","repo":"pola-rs/polars","slug":"invalid-predicate-for-filter-err","errorCode":null,"errorMessage":"invalid predicate for `filter`: {err}","messagePattern":"invalid predicate for `filter`: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/frame.py","lineNumber":4451,"sourceCode":"\n            # note: identify masks separately from predicates\n            if is_bool_sequence(p, include_series=True):\n                boolean_masks.append(pl.Series(p, dtype=Boolean))\n            elif (\n                (is_seq := is_sequence(p))\n                and any(not isinstance(x, pl.Expr) for x in p)\n            ) or (\n                not is_seq\n                and not isinstance(p, pl.Expr)\n                and not (isinstance(p, str) and p in self.collect_schema())\n            ):\n                err = (\n                    f\"Series(…, dtype={p.dtype})\"\n                    if isinstance(p, pl.Series)\n                    else repr(p)\n                )\n                msg = f\"invalid predicate for `filter`: {err}\"\n                raise TypeError(msg)\n            else:\n                all_predicates.extend(\n                    wrap_expr(x) for x in parse_into_list_of_expressions(p)\n                )\n\n        # unpack equality constraints from kwargs\n        all_predicates.extend(\n            F.col(name).eq(value) for name, value in constraints.items()\n        )\n        if not (all_predicates or boolean_masks):\n            msg = \"at least one predicate or constraint must be provided\"\n            raise TypeError(msg)\n\n        # if multiple predicates, combine as 'horizontal' expression\n        combined_predicate = (\n            (\n                F.all_horizontal(*all_predicates)\n                if len(all_predicates) > 1","sourceCodeStart":4433,"sourceCodeEnd":4469,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/lazyframe/frame.py#L4433-L4469","documentation":"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.","triggerScenarios":"`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.","commonSituations":"Passing pandas boolean masks instead of polars expressions; filtering by a numeric column expecting pandas-like truthiness; typos yielding non-expression objects.","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"],"exampleFix":"// before\ndf = lf.filter([True, False, True])\n\n// after\ndf = lf.filter(pl.Series([True, False, True]))  # boolean Series of matching length","handlingStrategy":"type-guard","validationCode":"def is_valid_predicate(p) -> bool:\n    return isinstance(p, (pl.Expr, bool, str)) or (\n        isinstance(p, pl.Series) and p.dtype == pl.Boolean\n    )","typeGuard":"def is_expr(p) -> bool:\n    return isinstance(p, pl.Expr)","tryCatchPattern":"try:\n    df = lf.filter(pred)\nexcept TypeError as e:\n    if \"invalid predicate for `filter`\" in str(e):\n        df = lf.filter(pl.lit(pred))\n    else:\n        raise","preventionTips":["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"],"tags":["python","polars","filter","predicate","type-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}