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

2023-11-22

我正在使用 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不能沮丧,除非你map or apply底层的闭包Series.

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

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


let my_expr = col("InvoiceDate").str().strptime(options);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Rust 中使用极地日期? 的相关文章

随机推荐