如何在 Rust 中使用极地日期?

How to use date in polars in rust?

我正在使用 LazyCsvReader 读取文件并且该文件包含一个日期列。 LazyCsvReader 将日期读取为字符串。日期格式为“%m-%d-%Y”。如何正确处理日期。有一个 page 用于此,但它用于 python。我试图阅读文档,但无法弄明白。以下是我无法编译的尝试案例

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

fn main() {
    let lf = read_csv_lazy("file.csv").unwrap();
    let out = lf.clone()
    .with_column((col("InvoiceDate").utf8().strptime("%m-%d-%Y")))
    .collect();
    println!("{:?}", out3);
}
fn read_csv_lazy(file_name: &str) -> Result<LazyFrame> {
    let lf: LazyFrame = LazyCsvReader::new(file_name.into())
                    .has_header(true)
                    .with_encoding(CsvEncoding::LossyUtf8)
                    .finish()?;
    Ok(lf)
}

我收到以下错误

error[E0599]: no method named `utf8` found for enum `Expr` in the current scope
  --> src/main.rs:20:38
   |
20 |     .with_column((col("InvoiceDate").utf8().strptime("%m-%d-%Y")))
   |                                      ^^^^ method not found in `Expr`

Polars Expressions 不能向下转换,除非你 mapapply 对基础 Series 关闭。

但是,在这种情况下您不需要任何关闭。您可以使用 str 命名空间,在 Expr::str() 方法下可用。

let options = StrpTimeOptions {
    fmt: Some("%m-%d-%Y".into()),
    ..Default::default()
};


let my_expr = col("InvoiceDate").str().strptime(options);