Search code examples
dataframerustrust-polars

remove duplicates in polars rust


hi i am trying to remove duplicates based on a column "time" in my dataframe.

i read the official document which defines the unique() method as follows:

pub fn unique(
    &self,
    subset: Option<&[String]>,
    keep: UniqueKeepStrategy,
    slice: Option<(i64, usize)>
) -> PolarsResult<DataFrame>
 

but somehow when i executed :

fn main() -> Result<(), Box<dyn Error>> {
    let col_name = String::from("time");
    let df = read_csv()?.unique(vec![col_name], UniqueKeep::First)?;
    println!("{:?}", df);
    Ok(())
}

i got this error:

error[E0433]: failed to resolve: use of undeclared type `UniqueKeep`
   --> src/main.rs:155:49
    |
155 |     let df = read_csv()?.unique(vec![col_name], UniqueKeep::First)?;
    |                                                 ^^^^^^^^^^ use of undeclared type `UniqueKeep`

could it be some kind of missing import? i only loaded the prelude:

use polars::prelude::*;

Solution

  • the enum should be UniqueKeepStrategy, and the slice could be simply 'None'

    also: use unique_stable instead to avoid order being meseed up.