Search code examples
rustrust-polars

Unable to access the column string namespace in rust polars. What did I miss?


I follow the example of https://docs.pola.rs/user-guide/expressions/strings/#__tabbed_2_2

Description

After I loaded my data from a local csv into a DataFrame, I am able to select a the specific FILE column and to print.

use polars::prelude::*;
use polars::prelude::DataFrame

<...>

let df_sim = match df_sim {
    Ok(df) => df,
    Err(e) => panic!("{}",e)
};

let df_new=df_sim
.clone()
.lazy()
.select([
    col("FILE")
    ]).collect();

println!("{:?}",df_new); 

┌───────────────┐
│ FILE          │
│ ---           │
│ str           │
╞═══════════════╡
│ 0.jpg         │
│ hello.jpg     │
│ harry.jpg     │
└───────────────┘

Problem

Now I wanted to look up for the file named harry in the FILE column. (Similar to the docs). Yet I am unable to access the str namespace? The compiler seem not to find any str() Method. Did I miss some polars settings?

let df_new=df_sim
.clone()
.lazy()
.select([
    col("FILE")
    .str()
    .contains(lit("harry"), false)
    .alias("regex"),
    ]).collect();

println!("{:?}",df_new); 

error[E0599]: no method named `str` found for enum `Expr` in the current scope
   |
61 | /         col("FILE")
62 | |         .str()
   | |         -^^^ help: there is a method with a similar name: `std`
   | |_________|
   |

My polars dependency in Cargo.toml:

polars = {version ="0.38.2", features = ["lazy"]}

Solution

  • You need to enable the feature strings for str() and regex for contains().