Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Add more examples for the rust (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Atreyagaurav authored Sep 13, 2023
1 parent 8193e78 commit 4b3847a
Show file tree
Hide file tree
Showing 17 changed files with 1,519 additions and 0 deletions.
186 changes: 186 additions & 0 deletions docs/src/rust/user-guide/expressions/casting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// --8<-- [start:setup]
use polars::{lazy::dsl::StrptimeOptions, prelude::*};
// --8<-- [end:setup]

fn main() -> Result<(), Box<dyn std::error::Error>> {
// --8<-- [start:dfnum]
let df = df! (
"integers"=> &[1, 2, 3, 4, 5],
"big_integers"=> &[1, 10000002, 3, 10000004, 10000005],
"floats"=> &[4.0, 5.0, 6.0, 7.0, 8.0],
"floats_with_decimal"=> &[4.532, 5.5, 6.5, 7.5, 8.5],
)?;

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

// --8<-- [start:castnum]
let out = df
.clone()
.lazy()
.select([
col("integers")
.cast(DataType::Float32)
.alias("integers_as_floats"),
col("floats")
.cast(DataType::Int32)
.alias("floats_as_integers"),
col("floats_with_decimal")
.cast(DataType::Int32)
.alias("floats_with_decimal_as_integers"),
])
.collect()?;
println!("{}", &out);
// --8<-- [end:castnum]

// --8<-- [start:downcast]
let out = df
.clone()
.lazy()
.select([
col("integers")
.cast(DataType::Int16)
.alias("integers_smallfootprint"),
col("floats")
.cast(DataType::Float32)
.alias("floats_smallfootprint"),
])
.collect();
match out {
Ok(out) => println!("{}", &out),
Err(e) => println!("{:?}", e),
};
// --8<-- [end:downcast]

// --8<-- [start:overflow]

let out = df
.clone()
.lazy()
.select([col("big_integers").strict_cast(DataType::Int8)])
.collect();
match out {
Ok(out) => println!("{}", &out),
Err(e) => println!("{:?}", e),
};
// --8<-- [end:overflow]

// --8<-- [start:overflow2]
let out = df
.clone()
.lazy()
.select([col("big_integers").cast(DataType::Int8)])
.collect();
match out {
Ok(out) => println!("{}", &out),
Err(e) => println!("{:?}", e),
};
// --8<-- [end:overflow2]

// --8<-- [start:strings]

let df = df! (
"integers" => &[1, 2, 3, 4, 5],
"float" => &[4.0, 5.03, 6.0, 7.0, 8.0],
"floats_as_string" => &["4.0", "5.0", "6.0", "7.0", "8.0"],
)?;

let out = df
.clone()
.lazy()
.select([
col("integers").cast(DataType::Utf8),
col("float").cast(DataType::Utf8),
col("floats_as_string").cast(DataType::Float64),
])
.collect()?;
println!("{}", &out);
// --8<-- [end:strings]

// --8<-- [start:strings2]

let df = df! ("strings_not_float"=> ["4.0", "not_a_number", "6.0", "7.0", "8.0"])?;

let out = df
.clone()
.lazy()
.select([col("strings_not_float").cast(DataType::Float64)])
.collect();
match out {
Ok(out) => println!("{}", &out),
Err(e) => println!("{:?}", e),
};
// --8<-- [end:strings2]

// --8<-- [start:bool]

let df = df! (
"integers"=> &[-1, 0, 2, 3, 4],
"floats"=> &[0.0, 1.0, 2.0, 3.0, 4.0],
"bools"=> &[true, false, true, false, true],
)?;

let out = df
.clone()
.lazy()
.select([
col("integers").cast(DataType::Boolean),
col("floats").cast(DataType::Boolean),
])
.collect()?;
println!("{}", &out);
// --8<-- [end:bool]

// --8<-- [start:dates]

use chrono::prelude::*;
use polars::time::*;

let df = df! (
"date" => date_range("date",
NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(), NaiveDate::from_ymd_opt(2022, 1, 5).unwrap().and_hms_opt(0, 0, 0).unwrap(), Duration::parse("1d"),ClosedWindow::Both, TimeUnit::Milliseconds, None)?.cast(&DataType::Date)?,
"datetime" => date_range("datetime",
NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(), NaiveDate::from_ymd_opt(2022, 1, 5).unwrap().and_hms_opt(0, 0, 0).unwrap(), Duration::parse("1d"),ClosedWindow::Both, TimeUnit::Milliseconds, None)?,
)?;

let out = df
.clone()
.lazy()
.select([
col("date").cast(DataType::Int64),
col("datetime").cast(DataType::Int64),
])
.collect()?;
println!("{}", &out);
// --8<-- [end:dates]

// --8<-- [start:dates2]

let df = df! (
"date" => date_range("date",
NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(), NaiveDate::from_ymd_opt(2022, 1, 5).unwrap().and_hms_opt(0, 0, 0).unwrap(), Duration::parse("1d"),ClosedWindow::Both, TimeUnit::Milliseconds, None)?,
"string" => &[
"2022-01-01",
"2022-01-02",
"2022-01-03",
"2022-01-04",
"2022-01-05",
],
)?;

let out = df
.clone()
.lazy()
.select([
col("date").dt().strftime("%Y-%m-%d"),
col("string").str().strptime(
DataType::Datetime(TimeUnit::Microseconds, None),
StrptimeOptions::default(),
),
])
.collect()?;
println!("{}", &out);
// --8<-- [end:dates2]

Ok(())
}
99 changes: 99 additions & 0 deletions docs/src/rust/user-guide/expressions/column-selections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// --8<-- [start:selectors_df]

use chrono::prelude::*;
use polars::time::*;

let df = df!(
"id" => &[9, 4, 2],
"place" => &["Mars", "Earth", "Saturn"],
"date" => date_range("date",
NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(), NaiveDate::from_ymd_opt(2022, 1, 3).unwrap().and_hms_opt(0, 0, 0).unwrap(), Duration::parse("1d"),ClosedWindow::Both, TimeUnit::Milliseconds, None)?,
"sales" => &[33.4, 2142134.1, 44.7],
"has_people" => &[false, true, false],
"logged_at" => date_range("logged_at",
NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(), NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 2).unwrap(), Duration::parse("1s"),ClosedWindow::Both, TimeUnit::Milliseconds, None)?,
)?
.with_row_count("rn", None)?;
println!("{}", &df);
// --8<-- [end:selectors_df]

// --8<-- [start:all]
let out = df.clone().lazy().select([col("*")]).collect()?;

// Is equivalent to
let out = df.clone().lazy().select([all()]).collect()?;
println!("{}", &out);
// --8<-- [end:all]

// --8<-- [start:exclude]
let out = df
.clone()
.lazy()
.select([col("*").exclude(["logged_at", "rn"])])
.collect()?;
println!("{}", &out);
// --8<-- [end:exclude]

// --8<-- [start:expansion_by_names]
let out = df
.clone()
.lazy()
.select([cols(["date", "logged_at"]).dt().to_string("%Y-%h-%d")])
.collect()?;
println!("{}", &out);
// --8<-- [end:expansion_by_names]

// --8<-- [start:expansion_by_regex]
let out = df.clone().lazy().select([col("^.*(as|sa).*$")]).collect()?;
println!("{}", &out);
// --8<-- [end:expansion_by_regex]

// --8<-- [start:expansion_by_dtype]
let out = df
.clone()
.lazy()
.select([dtype_cols([DataType::Int64, DataType::UInt32, DataType::Boolean]).n_unique()])
.collect()?;
// gives different result than python as the id col is i32 in rust
println!("{}", &out);
// --8<-- [end:expansion_by_dtype]

// --8<-- [start:selectors_intro]
// Not available in Rust, refer the following link
// https://github.com/pola-rs/polars/issues/10594
// --8<-- [end:selectors_intro]

// --8<-- [start:selectors_diff]
// Not available in Rust, refer the following link
// https://github.com/pola-rs/polars/issues/10594
// --8<-- [end:selectors_diff]

// --8<-- [start:selectors_union]
// Not available in Rust, refer the following link
// https://github.com/pola-rs/polars/issues/10594
// --8<-- [end:selectors_union]

// --8<-- [start:selectors_by_name]
// Not available in Rust, refer the following link
// https://github.com/pola-rs/polars/issues/1059
// --8<-- [end:selectors_by_name]

// --8<-- [start:selectors_to_expr]
// Not available in Rust, refer the following link
// https://github.com/pola-rs/polars/issues/10594
// --8<-- [end:selectors_to_expr]

// --8<-- [start:selectors_is_selector_utility]
// Not available in Rust, refer the following link
// https://github.com/pola-rs/polars/issues/10594
// --8<-- [end:selectors_is_selector_utility]

// --8<-- [start:selectors_colnames_utility]
// Not available in Rust, refer the following link
// https://github.com/pola-rs/polars/issues/10594
// --8<-- [end:selectors_colnames_utility]
Ok(())
}
79 changes: 79 additions & 0 deletions docs/src/rust/user-guide/expressions/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// --8<-- [start:dataframe]
use rand::{thread_rng, Rng};

let mut arr = [0f64; 5];
thread_rng().fill(&mut arr);

let df = df! (
"nrs" => &[Some(1), Some(2), Some(3), None, Some(5)],
"names" => &["foo", "ham", "spam", "egg", "spam"],
"random" => &arr,
"groups" => &["A", "A", "B", "C", "B"],
)?;

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

// --8<-- [start:samename]
let df_samename = df.clone().lazy().select([col("nrs") + lit(5)]).collect()?;
println!("{}", &df_samename);
// --8<-- [end:samename]

// --8<-- [start:samenametwice]
let df_samename2 = df
.clone()
.lazy()
.select([col("nrs") + lit(5), col("nrs") - lit(5)])
.collect();
match df_samename2 {
Ok(df) => println!("{}", &df),
Err(e) => println!("{:?}", &e),
};
// --8<-- [end:samenametwice]

// --8<-- [start:samenamealias]
let df_alias = df
.clone()
.lazy()
.select([
(col("nrs") + lit(5)).alias("nrs + 5"),
(col("nrs") - lit(5)).alias("nrs - 5"),
])
.collect()?;
println!("{}", &df_alias);
// --8<-- [end:samenamealias]

// --8<-- [start:countunique]
let df_alias = df
.clone()
.lazy()
.select([
col("names").n_unique().alias("unique"),
// Following query shows there isn't anything in Rust API
// https://docs.rs/polars/latest/polars/?search=approx_n_unique
// col("names").approx_n_unique().alias("unique_approx"),
])
.collect()?;
println!("{}", &df_alias);
// --8<-- [end:countunique]

// --8<-- [start:conditional]
let df_conditional = df
.clone()
.lazy()
.select([
col("nrs"),
when(col("nrs").gt(2))
.then(lit(true))
.otherwise(lit(false))
.alias("conditional"),
])
.collect()?;
println!("{}", &df_conditional);
// --8<-- [end:conditional]

Ok(())
}
Loading

0 comments on commit 4b3847a

Please sign in to comment.