我正在使用 LazyCsvReader 读取文件,并且该文件包含日期列。LazyCsvReader 将日期读取为字符串。日期的格式为“%m-%d-%Y”。如何正确处理日期。有一个页面是针对这个的,但它是针对 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)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
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`
Run Code Online (Sandbox Code Playgroud) 我有一个数据集,需要对不同列进行分组操作。这是使用极地版本“0.21.1”的最小工作代码
use polars::prelude::*;
use polars_lazy::prelude::*;
use polars::df;
fn main(){
let df = df![
"x1" => ["a", "b", "c", "a"],
"x2" => ["A", "A", "B", "B"],
"y" => [1, 2, 3, 4],
].unwrap();
let lf: LazyFrame = df.lazy();
let out1 = groupby_x1(&lf);
println!("{:?}", out1.collect());
let out2 = groupby_x2(&lf);
println!("{:?}", out2.collect());
}
fn groupby_x1(lf: &LazyFrame) -> LazyFrame {
let lf1: LazyFrame = lf.clone().groupby([col("x1")]).agg([
col("y").sum().alias("y_sum"),
]);
lf1
}
fn groupby_x2(lf: &LazyFrame) -> LazyFrame {
let lf1: LazyFrame = lf.clone().groupby([col("x2")]).agg([
col("y").sum().alias("y_sum"),
]);
lf1
} …Run Code Online (Sandbox Code Playgroud)