Skip to content

Commit

Permalink
docs: Add more Rust examples to User Guide (#20194)
Browse files Browse the repository at this point in the history
Co-authored-by: Rodrigo Girão Serrão <5621605+rodrigogiraoserrao@users.noreply.github.com>
  • Loading branch information
lukemanley and rodrigogiraoserrao authored Dec 8, 2024
1 parent d4bab3f commit 0269f64
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 31 deletions.
4 changes: 2 additions & 2 deletions docs/source/src/python/user-guide/expressions/missing-data.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
# --8<-- [end:fill]

# --8<-- [start:fillexpr]
fill_median_df = df.with_columns(
fill_expression_df = df.with_columns(
pl.col("col2").fill_null((2 * pl.col("col1")).cast(pl.Int64)),
)
print(fill_median_df)
print(fill_expression_df)
# --8<-- [end:fillexpr]

# --8<-- [start:fillstrategy]
Expand Down
4 changes: 2 additions & 2 deletions docs/source/src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ required-features = ["polars/lazy"]
[[bin]]
name = "expressions-missing-data"
path = "user-guide/expressions/missing-data.rs"
required-features = ["polars/lazy"]
required-features = ["polars/lazy", "polars/interpolate"]
[[bin]]
name = "expressions-operations"
path = "user-guide/expressions/operations.rs"
required-features = ["polars/lazy", "polars/approx_unique", "polars/dtype-struct", "polars/unique_counts"]
[[bin]]
name = "expressions-strings"
path = "user-guide/expressions/strings.rs"
required-features = ["polars/lazy"]
required-features = ["polars/lazy", "polars/strings", "polars/regex"]
[[bin]]
name = "expressions-structs"
path = "user-guide/expressions/structs.rs"
Expand Down
20 changes: 17 additions & 3 deletions docs/source/src/rust/user-guide/expressions/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let dataset = CsvReadOptions::default()
.with_has_header(true)
.with_schema(Some(Arc::new(schema)))
.with_schema_overwrite(Some(Arc::new(schema)))
.map_parse_options(|parse_options| parse_options.with_try_parse_dates(true))
.into_reader_with_file_handle(Cursor::new(data))
.finish()?;
Expand Down Expand Up @@ -88,7 +88,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.clone()
.lazy()
.group_by(["state", "party"])
.agg([len().count().alias("count")])
.agg([len().alias("count")])
.filter(
col("party")
.eq(lit("Anti-Administration"))
Expand Down Expand Up @@ -135,7 +135,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// --8<-- [end:filter]

// --8<-- [start:filter-nested]
// Contribute the Rust translation of the Python example by opening a PR.
let df = dataset
.clone()
.lazy()
.group_by(["state", "gender"])
.agg([compute_age().mean().alias("avg birthday"), len().alias("#")])
.sort(
["#"],
SortMultipleOptions::default()
.with_order_descending(true)
.with_nulls_last(true),
)
.limit(5)
.collect()?;

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

// --8<-- [start:sort]
Expand Down
86 changes: 81 additions & 5 deletions docs/source/src/rust/user-guide/expressions/folds.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,99 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
// --8<-- [start:mansum]
use polars::lazy::dsl::sum_horizontal;
use polars::prelude::*;
// Contribute the Rust translation of the Python example by opening a PR.

let df = df!(
"label" => ["foo", "bar", "spam"],
"a" => [1, 2, 3],
"b" => [10, 20, 30],
)?;

let result = df
.clone()
.lazy()
.select([
fold_exprs(
lit(0),
|acc, val| (&acc + &val).map(Some),
[col("a"), col("b")],
)
.alias("sum_fold"),
sum_horizontal([col("a"), col("b")], true)?.alias("sum_horz"),
])
.collect()?;

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

// --8<-- [start:mansum-explicit]
// Contribute the Rust translation of the Python example by opening a PR.
let acc = lit(0);
let f = |acc: Expr, val: Expr| acc + val;

let result = df
.clone()
.lazy()
.select([
f(f(acc, col("a")), col("b")),
fold_exprs(
lit(0),
|acc, val| (&acc + &val).map(Some),
[col("a"), col("b")],
)
.alias("sum_fold"),
])
.collect()?;

println!("{:?}", result);
// --8<-- [end:mansum-explicit]

// --8<-- [start:manprod]
// Contribute the Rust translation of the Python example by opening a PR.
let result = df
.clone()
.lazy()
.select([fold_exprs(
lit(0),
|acc, val| (&acc * &val).map(Some),
[col("a"), col("b")],
)
.alias("prod")])
.collect()?;

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

// --8<-- [start:manprod-fixed]
// Contribute the Rust translation of the Python example by opening a PR.
let result = df
.clone()
.lazy()
.select([fold_exprs(
lit(1),
|acc, val| (&acc * &val).map(Some),
[col("a"), col("b")],
)
.alias("prod")])
.collect()?;

println!("{:?}", result);
// --8<-- [end:manprod-fixed]

// --8<-- [start:conditional]
// Contribute the Rust translation of the Python example by opening a PR.
let df = df!(
"a" => [1, 2, 3],
"b" => [0, 1, 2],
)?;

let result = df
.clone()
.lazy()
.filter(fold_exprs(
lit(true),
|acc, val| (&acc & &val).map(Some),
[col("*").gt(1)],
))
.collect()?;

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

// --8<-- [start:string]
Expand Down
74 changes: 66 additions & 8 deletions docs/source/src/rust/user-guide/expressions/missing-data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,96 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// --8<-- [end:isnull]

// --8<-- [start:dataframe2]
// Contribute the Rust translation of the Python example by opening a PR.
let df = df! (
"col1" => [0.5, 1.0, 1.5, 2.0, 2.5],
"col2" => [Some(1), None, Some(3), None, Some(5)],
)?;

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

// --8<-- [start:fill]
// Contribute the Rust translation of the Python example by opening a PR.
let fill_literal_df = df
.clone()
.lazy()
.with_column(col("col2").fill_null(3))
.collect()?;

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

// --8<-- [start:fillstrategy]
// Contribute the Rust translation of the Python example by opening a PR.

let fill_literal_df = df
.clone()
.lazy()
.with_columns([
col("col2")
.fill_null_with_strategy(FillNullStrategy::Forward(None))
.alias("forward"),
col("col2")
.fill_null_with_strategy(FillNullStrategy::Backward(None))
.alias("backward"),
])
.collect()?;

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

// --8<-- [start:fillexpr]
// Contribute the Rust translation of the Python example by opening a PR.
let fill_expression_df = df
.clone()
.lazy()
.with_column(col("col2").fill_null((lit(2) * col("col1")).cast(DataType::Int64)))
.collect()?;

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

// --8<-- [start:fillinterpolate]
// Contribute the Rust translation of the Python example by opening a PR.
let fill_interpolation_df = df
.clone()
.lazy()
.with_column(col("col2").interpolate(InterpolationMethod::Linear))
.collect()?;

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

// --8<-- [start:nan]
let nan_df = df!(
"value" => [1.0, f64::NAN, f64::NAN, 3.0],
"value" => [1.0, f64::NAN, f64::NAN, 3.0],
)?;
println!("{}", nan_df);
// --8<-- [end:nan]

// --8<-- [start:nan-computed]
// Contribute the Rust translation of the Python example by opening a PR.
let df = df!(
"dividend" => [1.0, 0.0, -1.0],
"divisor" => [1.0, 0.0, -1.0],
)?;

let result = df
.clone()
.lazy()
.select([col("dividend") / col("divisor")])
.collect()?;

println!("{}", result);
// --8<-- [end:nan-computed]

// --8<-- [start:nanfill]
// Contribute the Rust translation of the Python example by opening a PR.
let mean_nan_df = nan_df
.clone()
.lazy()
.with_column(col("value").fill_nan(Null {}.lit()).alias("replaced"))
.select([
col("*").mean().name().suffix("_mean"),
col("*").sum().name().suffix("_sum"),
])
.collect()?;

println!("{}", mean_nan_df);
// --8<-- [end:nanfill]
Ok(())
}
Loading

0 comments on commit 0269f64

Please sign in to comment.