Home > Mobile >  Rust Polars: how to change column value from string to datetime
Rust Polars: how to change column value from string to datetime

Time:10-08

I'm reading my CSV file with polars, but there's a column with values that I need to convert to DateTime. I've managed to manipulate the String (remove dots) now I need to strptime to convert to DateTime. How can I do this in rust polars?

My old project in Python did this, but I want to rewrite this in Rust Polars: df["Meetdatum"] = df["Meetdatum"].str.replace(".", "", regex=False)

df["Datum"] = pd.to_datetime(df["Meetdatum"], format="%d %b %Y")

CodePudding user response:

You can use something like this:

Cargo.toml:

[dependencies]
    polars = { version = "*", features = [
        "lazy",
        "dtype-datetime",
        "strings",
        "lazy_regex",
    ] }

main.rs:

use polars::prelude::*;

fn main() {
    let df = DataFrame::new(vec![Series::new(
        "date_str",
        &[
            "5. Oct, ...2022",
            "..31 ..Oct.., 2022.",
            "E.R.R.O.R",
            "31 Sep, 2022",
        ],
    )])
    .unwrap()
    .lazy();

    let df = df.with_column(
        col("date_str")
            .str()
            .replace_all(lit("."), lit(""), true)
            .str()
            .strptime(StrpTimeOptions {
                date_dtype: DataType::Date,
                fmt: Some("%d %b, %Y".into()),
                strict: false,
                exact: true,
            })
            .alias("date"),
    );

    println!("{:?}", df.collect().unwrap());
}

Result:

shape: (4, 2)
┌─────────────────────┬────────────┐
│ date_str            ┆ date       │
│ ---                 ┆ ---        │
│ str                 ┆ date       │
╞═════════════════════╪════════════╡
│ 5. Oct, ...2022     ┆ 2022-10-05 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ ..31 ..Oct.., 2022. ┆ 2022-10-31 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ E.R.R.O.R           ┆ null       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 31 Sep, 2022        ┆ null       │
└─────────────────────┴────────────┘
  • Related