1

I have been trying many examples to learn to use polars with dates, but every example uses StrpTimeOptions and I can't find any crate or features that bring that into scope. I also cannot find any documentation on this. Some of the examples that I have tried are here, here, and here.

Here is the primary error I am getting:

error[E0422]: cannot find struct, variant or union type `StrpTimeOptions` in this scope
  --> src/main.rs:26:23
   |
26 |             .strptime(StrpTimeOptions {
   |                       ^^^^^^^^^^^^^^^ help: a struct with a similar name exists: `StrptimeOptions`

Here is the code example I am trying to compile:

use polars::prelude::*;
use polars_lazy::prelude::*;
use chrono::prelude::*;

fn main() {

    // https://stackoverflow.com/questions/73965088/rust-polars-how-to-change-column-value-from-string-to-datetime
    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());

    // // https://stackoverflow.com/questions/72276752/how-to-use-date-in-polars-in-rust
    // let days = df!("column_1" => &["Tuesday"],
    //     "column_2" => &["1900-01-02"]);
    // println!("{:?}", days.unwrap());

    // let options = StrpTimeOptions {
    //     fmt: Some("%m-%d-%Y".into()),
    //     ..Default::default()
    // };
    
    // let my_expr = col("column_2").str().strptime(options);


    // // https://stackoverflow.com/questions/74522375/polars-rust-formatting-a-duration-to-a-string
    // let df = df! {
    //     "a" => ["2022-11-21T12:00:00"],
    //     "b" => ["2022-11-21T14:00:00"]
    //   }
    //   .unwrap()
    //   .lazy()
    //   .with_column(
    //       col("a")
    //           .str()
    //           .strptime(StrpTimeOptions {
    //               date_dtype: DataType::Datetime(TimeUnit::Milliseconds, None),
    //               fmt: Some("%Y-%m-%dT%H:%M:%S".into()),
    //               strict: false,
    //               exact: true,
    //           })
    //           .alias("a"),
    //   )
    //   .with_column(
    //       col("b")
    //           .str()
    //           .strptime(StrpTimeOptions {
    //               date_dtype: DataType::Datetime(TimeUnit::Milliseconds, None),
    //               fmt: Some("%Y-%m-%dT%H:%M:%S".into()),
    //               strict: false,
    //               exact: true,
    //           })
    //           .alias("b"),
    //   )
    //   .with_column((col("b") - col("a")).alias("duration"))
    //   .collect()
    //   .unwrap();
  
    //   println!("{:?}", df);
    

}
Roger Bos
  • 11
  • 1
  • 4
    It's called [`StrptimeOptions`](https://docs.rs/polars/0.30.0/polars/prelude/struct.StrptimeOptions.html) (lower-case "t") in the latest version of polars (it was called [`StrpTimeOptions`](https://docs.rs/polars/0.27.1/polars/prelude/struct.StrpTimeOptions.html) in older versions) – Jmb Jun 13 '23 at 06:43
  • @Jmb Thank you. That's what I needed to know. – Roger Bos Jun 13 '23 at 16:15

0 Answers0