我正在读取一个以月-日-年格式显示日期的csv文件(例如"2022年11月15日").但月份和日期没有0填充.下面是我的测试代码
use polars::prelude::*;
use polars_lazy::prelude::*;
fn main() {
let df = df![
"x" => ["1/4/2011", "2/4/2011", "3/4/2011", "4/4/2011"],
"y" => [1, 2, 3, 4],
].unwrap();
let lf: LazyFrame = df.lazy();
let options = StrpTimeOptions {
fmt: Some("%m/%d/%Y".into()),
date_dtype: DataType::Date,
..Default::default()
};
let res = lf.clone()
.with_column(col("x").str().strptime(options).alias("new time"))
.collect().unwrap();
println!("{:?}", res);
}
输出为
shape: (4, 3)
┌──────────┬─────┬──────────┐
│ x ┆ y ┆ new time │
│ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ date │
╞══════════╪═════╪══════════╡
│ 1/4/2011 ┆ 1 ┆ null │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 2/4/2011 ┆ 2 ┆ null │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 3/4/2011 ┆ 3 ┆ null │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 4/4/2011 ┆ 4 ┆ null │
在options
中,我try 了"%-m/%-d/%Y
,而不是documentation中提到的"%m/%d/%Y
.但它在运行时感到panic .
thread '<unnamed>' panicked at 'attempt to subtract with overflow', /home/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/polars-time-0.21.1/src/chunkedarray/utf8/mod.rs:234:33
阅读此格式的正确方法是什么.我正在使用"Ubuntu 20.04.4 LTS"