This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rust examples for time-series resampling and timezones
- Loading branch information
1 parent
d741489
commit eb4859c
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
docs/src/rust/user-guide/transformations/time-series/resampling.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<dyn std::error::Error>> { | ||
// --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(()) | ||
} |
46 changes: 46 additions & 0 deletions
46
docs/src/rust/user-guide/transformations/time-series/timezones.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// --8<-- [start:setup] | ||
use polars::prelude::*; | ||
// --8<-- [end:setup] | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// --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(()) | ||
} |