我正在使用極坐標讀取我的 CSV 檔案,但是有一列包含我需要轉換為 DateTime 的值。我已經設法操縱字串(洗掉點),現在我需要 strptime 轉換為 DateTime。我怎樣才能在生銹的極地做到這一點?
我在 Python 中的舊專案就是這樣做的,但我想在 Rust Polars 中重寫它:
df["Meetdatum"] = df["Meetdatum"].str.replace(".", "", regex=False)
df["Datum"] = pd.to_datetime(df["Meetdatum"], format="%d %b %Y")
uj5u.com熱心網友回復:
你可以使用這樣的東西:
貨物.toml:
[dependencies]
polars = { version = "*", features = [
"lazy",
"dtype-datetime",
"strings",
"lazy_regex",
] }
main.rs:
use polars::prelude::*;
fn main() {
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());
}
結果:
shape: (4, 2)
┌─────────────────────┬────────────┐
│ date_str ┆ date │
│ --- ┆ --- │
│ str ┆ date │
╞═════════════════════╪════════════╡
│ 5. Oct, ...2022 ┆ 2022-10-05 │
├?????????????????????┼????????????┤
│ ..31 ..Oct.., 2022. ┆ 2022-10-31 │
├?????????????????????┼????????????┤
│ E.R.R.O.R ┆ null │
├?????????????????????┼????????????┤
│ 31 Sep, 2022 ┆ null │
└─────────────────────┴────────────┘
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/512097.html
標籤:约会时间锈字符串时间
上一篇:使用范圍遞增日期時間物件
