From eb4859c1fc50ce3111ddcc58c46745f3f2ca21c3 Mon Sep 17 00:00:00 2001 From: Gaurav-Lab-PC Date: Fri, 1 Sep 2023 15:55:36 -0400 Subject: [PATCH] Add rust examples for time-series resampling and timezones --- .../transformations/time-series/resampling.rs | 43 +++++++++++++++++ .../transformations/time-series/timezones.rs | 46 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 docs/src/rust/user-guide/transformations/time-series/resampling.rs create mode 100644 docs/src/rust/user-guide/transformations/time-series/timezones.rs diff --git a/docs/src/rust/user-guide/transformations/time-series/resampling.rs b/docs/src/rust/user-guide/transformations/time-series/resampling.rs new file mode 100644 index 000000000..60888c264 --- /dev/null +++ b/docs/src/rust/user-guide/transformations/time-series/resampling.rs @@ -0,0 +1,43 @@ +// --8<-- [start:setup] +use chrono::prelude::*; +use polars::io::prelude::*; +use polars::prelude::*; +use polars::time::prelude::*; +// --8<-- [end:setup] + +fn main() -> Result<(), Box> { + // --8<-- [start:df] + let df = df!( + "time" => date_range( + "time", + NaiveDate::from_ymd_opt(2021, 12, 16).unwrap().and_hms_opt(0, 0, 0).unwrap(), + NaiveDate::from_ymd_opt(2021, 12, 16).unwrap().and_hms_opt(3, 0, 0).unwrap(), + Duration::parse("30m"), + ClosedWindow::Both, + TimeUnit::Milliseconds, None)?, + "groups" => &["a", "a", "a", "b", "b", "a", "a"], + "values" => &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], + )?; + println!("{}", &df); + // --8<-- [end:df] + + // --8<-- [start:upsample] + let out1 = df + .clone() + .upsample::<[String; 0]>([], "time", Duration::parse("15m"), Duration::parse("0"))? + .fill_null(FillNullStrategy::Forward(None))?; + println!("{}", &out1); + // --8<-- [end:upsample] + + // --8<-- [start:upsample2] + let out2 = df + .clone() + .upsample::<[String; 0]>([], "time", Duration::parse("15m"), Duration::parse("0"))? + .lazy() + .with_columns([col("values").interpolate(InterpolationMethod::Linear)]) + .collect()? + .fill_null(FillNullStrategy::Forward(None))?; + println!("{}", &out2); + // --8<-- [end:upsample2] + Ok(()) +} diff --git a/docs/src/rust/user-guide/transformations/time-series/timezones.rs b/docs/src/rust/user-guide/transformations/time-series/timezones.rs new file mode 100644 index 000000000..20f818954 --- /dev/null +++ b/docs/src/rust/user-guide/transformations/time-series/timezones.rs @@ -0,0 +1,46 @@ +// --8<-- [start:setup] +use polars::prelude::*; +// --8<-- [end:setup] + +fn main() -> Result<(), Box> { + // --8<-- [start:example] + let ts = ["2021-03-27 03:00", "2021-03-28 03:00"]; + let tz_naive = Series::new("tz_naive", &ts); + let time_zones_df = DataFrame::new(vec![tz_naive])? + .lazy() + .select([col("tz_naive").str().strptime( + DataType::Datetime(TimeUnit::Milliseconds, None), + StrptimeOptions::default(), + )]) + .with_columns([col("tz_naive") + .dt() + .replace_time_zone(Some("UTC".to_string()), None) + .alias("tz_aware")]) + .collect()?; + + println!("{}", &time_zones_df); + // --8<-- [end:example] + + // --8<-- [start:example2] + let time_zones_operations = time_zones_df + .lazy() + .select([ + col("tz_aware") + .dt() + .replace_time_zone(Some("Europe/Brussels".to_string()), None) + .alias("replace time zone"), + col("tz_aware") + .dt() + .convert_time_zone("Asia/Kathmandu".to_string()) + .alias("convert time zone"), + col("tz_aware") + .dt() + .replace_time_zone(None, None) + .alias("unset time zone"), + ]) + .collect()?; + println!("{}", &time_zones_operations); + // --8<-- [end:example2] + + Ok(()) +}