Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polars: Addressing "The predicate passed to 'LazyFrame.filter' expanded to multiple expressions"

From Polars 0.16.11 the following filter statement raises an exception with the following error message:

import polars as pl
df = pl.DataFrame({'lag1':[0,1,None],'lag2':[0,None,2]})
df.filter(pl.col('^lag.*$').is_not_null())

ComputeError: The predicate passed to 'LazyFrame.filter' expanded to multiple expressions:

    col("lag1").is_not_null().any(),
    col("lag2").is_not_null().any(),
This is ambiguous. Try to combine the predicates with the 'all' or `any' expression.

How can I address it to filter rows where any of the lag columns are null?

like image 411
braaannigan Avatar asked Dec 08 '25 06:12

braaannigan


1 Answers

As @lxlxlx rightfully mentioned, as of today's Polars API, the correct way to evaluate against a predicate containing multiple Boolean expressions is either by using:

  • pl.all_horizontal() (for bitwise AND horizontally across expressions)
  • pl.any_horizontal() (for bitwise OR horizontally across expressions).
like image 149
Omar AlSuwaidi Avatar answered Dec 09 '25 19:12

Omar AlSuwaidi