Home > OS >  Rust Polars filter string input eagerly
Rust Polars filter string input eagerly

Time:11-04

I am trying to understand the 2 modes (lazy, eager) in Polars. I Found a way to do this lazily:

let out = df
        .lazy()
        .select([col("A"), col("B")
        .filter(col("B").str().contains(input))])
        .collect()
        .unwrap().head(Some(10));

But how to do this eagerly? There is an example:

https://docs.rs/polars/latest/polars/docs/eager/index.html#filter which I couldn't make work with str().contains(). And Do I understand correctly that col("") is specific for lazy and column("") for eager?

// create a mask to filter out null values
let mask = df.column("sepal.width")?.is_not_null();

// apply the filter on a DataFrame
let filtered_df = df.filter(&mask)?;

CodePudding user response:

On a Series, the method is not called str like on Exprs, but utf8 (see https://docs.rs/polars/latest/polars/series/struct.Series.html#method.utf8). In your case, you would use it like this:

let filtered_df = df.filter(&df.column("B")?.utf8()?.contains(input)?)?;

Note that there are multiple uses of the ? operator which require that your function returns a compatible Result (such as a PolarsResult).

  • Related