Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Feb 7, 2024
1 parent 5a1be08 commit 7d92596
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
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
82 changes: 39 additions & 43 deletions docs/src/rust/user-guide/basics/series-dataframes.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
use polars::prelude::*;
fn main() {
// --8<-- [start:series]
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let s = Series::new("a", &[1, 2, 3, 4, 5]);

// --8<-- [start:series]
let s = Series::new("a", &[1, 2, 3, 4, 5])
println!("{}", s);
// --8<-- [end:series]

println!("{}", s)
// --8<-- [end:series]
// --8<-- [start:dataframe]
use chrono::NaiveDate;

// --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();

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]

println!("{}", df)
// --8<-- [end:dataframe]
// --8<-- [start:head]
let df_head = df.head(Some(3));

// --8<-- [start:head]
let df_head = df.head(Some(3))
println!("{}", df_head);
// --8<-- [end:head]

println("{}", df_head)
// --8<-- [end:head]
// --8<-- [start:tail]
let df_tail = df.tail(Some(3));

// --8<-- [start:tail]
let df_tail = df.tail(Some(3))
println!("{}", df_tail);
// --8<-- [end:tail]

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();

// --8<-- [start:sample]
let s1 = Series::new("", &[2]);
println!("{}", sampled_df);
// --8<-- [end:sample]

let sampled_df = df.sample_n(&s1, false, false, None);

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

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

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

0 comments on commit 7d92596

Please sign in to comment.