Search code examples
rustrust-polars

Rust Polars - Selecting columns after applying filter (on rows) of a DataFrame


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.


Solution

  • 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.