【发布时间】:2022-08-16 03:51:00
【问题描述】:
我想这是一个概念上的矛盾“在 LazyFrame 列中窥视”......也许你们中的一个人可以启发我如何最好地做到这一点。
我想将每个日期的结果放入一个新列中:
好的((下一个工作日编号 - 当前工作日编号)== 1)
这是帮助我找到答案的示例代码:
// PLEASE be aware to add the needed feature flags in your toml file use polars::export::arrow::temporal_conversions::date32_to_date; use polars::prelude::*; fn main() -> Result<()> { let days = df!( \"date_string\" => &[\"1900-01-01\", \"1900-01-02\", \"1900-01-03\", \"1900-01-04\", \"1900-01-05\", \"1900-01-06\", \"1900-01-07\", \"1900-01-09\", \"1900-01-10\"])?; let options = StrpTimeOptions { date_dtype: DataType::Date, // the result column-datatype fmt: Some(\"%Y-%m-%d\".into()), // the source format of the date-string strict: false, exact: true, }; // convert date_string into dtype(date) and put into new column \"date_type\" // we convert the days DataFrame to a LazyFrame ... // because in my real-world example I am getting a LazyFrame let mut new_days = days.lazy().with_column( col(\"date_string\") .alias(\"date_type\") .str() .strptime(options), ); // This is what I wanted to do ... but I get a string result .. need u32 // let o = GetOutput::from_type(DataType::Date); // new_days = new_days.with_column( // col(\"date_type\") // .alias(\"weekday_number\") // .map(|x| Ok(x.strftime(\"%w\").unwrap()), o.clone()), // ); // This is the convoluted workaround let o = GetOutput::from_type(DataType::Date); new_days = new_days.with_column(col(\"date_type\").alias(\"weekday_number\").map( |x| { Ok(x.date() .unwrap() .clone() .into_iter() .map(|opt_name: Option<i32>| { opt_name.map(|datum: i32| { // println!(\"{:?}\", datum); date32_to_date(datum) .format(\"%w\") .to_string() .parse::<u32>() .unwrap() }) }) .collect::<UInt32Chunked>() .into_series()) }, o, )); // Here is where my challenge is .. // I need to get the weekday_number of the following day to determine a condition // my pseudo code: // new_days = new_days.with_column( // col(\"weekday_number\") // .alias(\"cold_day\") // .map(|x| Ok( (next_weekday_number - current_weekday_number) == 1 ), o.clone()), // ); println!(\"{:?}\", new_days.clone().collect()); Ok(()) }
-
我能够将 current_wk_day_num 列移位 -1 .>>> \"new_days = new_days.with_column(col(\"weekday_number\").alias(\"next_weekday_number\").shift(-1)); \" <<< .... 因此在所有行中都有我需要的信息。现在我的挑战是在逻辑上组合两列并将结果放入一个新列中..
标签: rust peek rust-polars