Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Rust examples for data structures in user guide #14339

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]

Comment on lines -8 to -28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These code examples were unused.

# --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]
}
Loading