Skip to content

Commit

Permalink
docs: Rust examples for data structures in user guide (#14339)
Browse files Browse the repository at this point in the history
Co-authored-by: Stijn de Gooijer <stijndegooijer@gmail.com>
  • Loading branch information
r-brink and stinodego committed Feb 7, 2024
1 parent ac673b5 commit 5120656
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
21 changes: 0 additions & 21 deletions docs/src/python/user-guide/basics/series-dataframes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,6 @@
print(s)
# --8<-- [end:series]

# --8<-- [start:minmax]
s = pl.Series("a", [1, 2, 3, 4, 5])
print(s.min())
print(s.max())
# --8<-- [end:minmax]

# --8<-- [start:string]
s = pl.Series("a", ["polar", "bear", "arctic", "polar fox", "polar bear"])
s2 = s.str.replace("polar", "pola")
print(s2)
# --8<-- [end:string]

# --8<-- [start:dt]
from datetime import date

start = date(2001, 1, 1)
stop = date(2001, 1, 9)
s = pl.date_range(start, stop, interval="2d", eager=True)
print(s.dt.day())
# --8<-- [end:dt]

# --8<-- [start:dataframe]
from datetime import datetime

Expand Down
3 changes: 3 additions & 0 deletions docs/src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ path = "user-guide/basics/joins.rs"
name = "user-guide-basics-reading-writing"
path = "user-guide/basics/reading-writing.rs"
required-features = ["polars/json"]
[[bin]]
name = "user-guide-basics-series-dataframes"
path = "user-guide/basics/series-dataframes.rs"

[[bin]]
name = "user-guide-concepts-contexts"
Expand Down
51 changes: 51 additions & 0 deletions docs/src/rust/user-guide/basics/series-dataframes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
fn main() {
// --8<-- [start:series]
use polars::prelude::*;

let s = Series::new("a", &[1, 2, 3, 4, 5]);

println!("{}", s);
// --8<-- [end:series]

// --8<-- [start:dataframe]
use chrono::NaiveDate;

let df: DataFrame = df!(
"integer" => &[1, 2, 3, 4, 5],
"date" => &[
NaiveDate::from_ymd_opt(2025, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(),
NaiveDate::from_ymd_opt(2025, 1, 2).unwrap().and_hms_opt(0, 0, 0).unwrap(),
NaiveDate::from_ymd_opt(2025, 1, 3).unwrap().and_hms_opt(0, 0, 0).unwrap(),
NaiveDate::from_ymd_opt(2025, 1, 4).unwrap().and_hms_opt(0, 0, 0).unwrap(),
NaiveDate::from_ymd_opt(2025, 1, 5).unwrap().and_hms_opt(0, 0, 0).unwrap(),
],
"float" => &[4.0, 5.0, 6.0, 7.0, 8.0]
)
.unwrap();

println!("{}", df);
// --8<-- [end:dataframe]

// --8<-- [start:head]
let df_head = df.head(Some(3));

println!("{}", df_head);
// --8<-- [end:head]

// --8<-- [start:tail]
let df_tail = df.tail(Some(3));

println!("{}", df_tail);
// --8<-- [end:tail]

// --8<-- [start:sample]
let n = Series::new("", &[2]);
let sampled_df = df.sample_n(&n, false, false, None).unwrap();

println!("{}", sampled_df);
// --8<-- [end:sample]

// --8<-- [start:describe]
// Not available in Rust
// --8<-- [end:describe]
}

0 comments on commit 5120656

Please sign in to comment.