0

This used to be working until upgraded to latest Polars. The df Dataframe has more than 2 columns, CP and CP are 2 of them. The intent here is to select column CK and CP for rows that are greater than value 0.

let e11_filter = df
    .lazy()
    .filter(cols(["CK", "CP"]).gt(lit(0)))
    .collect()?;

The error is as below.

Error: ComputeError(ErrString("The predicate passed to 'LazyFrame.filter' expanded to multiple expressions:

    [(col(\"CK\")) > (0)],
    [(col(\"CP\")) > (0)],
This is ambiguous. Try to combine the predicates with the 'all_exprs' or 'any_exprs' expression.

Error originated just after this operation:
DF [\"W\", \"Dataset\", \"MD\", \"CK\"]; PROJECT */8 COLUMNS; SELECTION: \"None\""))

I found similar question, but it is Python version.

Jmb
  • 18,893
  • 2
  • 28
  • 55
  • Do you want rows which have both CK > 0 and CP > 0 or either one of them > 0? – Dogbert Jun 09 '23 at 07:29
  • Both rows in CK and CP are greater than 0, not either. – humphreylee Jun 09 '23 at 07:37
  • Can you try `.filter(all_exprs(cols(["CK", "CP"]).gt(lit(0))))`? https://docs.rs/polars/latest/polars/prelude/fn.all_exprs.html – Dogbert Jun 09 '23 at 07:47
  • Tried that. Error ```error[E0277]: the trait bound 'Expr: AsRef<[Expr]> is not satisfied --> src\main.rs:50:27 | 50 | .filter(all_exprs(cols(["CK", "CP"]).gt(lit(0)))) | --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait 'AsRef<[Expr]>' is not implemented for 'Expr' | | | required by a bound introduced by this call | note: required by a bound in 'polars::prelude::all_exprs'``` – humphreylee Jun 09 '23 at 07:52
  • Here is part of my Cargo.toml ```polars = {version = "*", features =["lazy", "ndarray"]}``` – humphreylee Jun 09 '23 at 07:57

1 Answers1

0

I'd make the call unambiguous by applying the "greater than 0" filter on each column explicitly and combining these resulting column filters with the and operation:

let e11_filter = df
    .lazy()
    .filter(col("CK").gt(lit(0)).and(col("CP").gt(lit(0))))
    .collect()
    .unwrap();

Rustexplorer.


Update: I got the all_exprs version Dogbert suggested to work, which is more in line with the answer of the Python version you've linked:

let e11_filter = df
    .lazy()
    .filter(all_exprs([cols(["CK", "CP"]).gt(lit(0))]))
    .collect()
    .unwrap();

Rustexplorer.

Jonas Fassbender
  • 2,371
  • 1
  • 3
  • 19