Skip to content

Commit 0269f64

Browse files
docs: Add more Rust examples to User Guide (#20194)
Co-authored-by: Rodrigo Girão Serrão <5621605+rodrigogiraoserrao@users.noreply.github.com>
1 parent d4bab3f commit 0269f64

File tree

7 files changed

+315
-31
lines changed

7 files changed

+315
-31
lines changed

docs/source/src/python/user-guide/expressions/missing-data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
# --8<-- [end:fill]
4444

4545
# --8<-- [start:fillexpr]
46-
fill_median_df = df.with_columns(
46+
fill_expression_df = df.with_columns(
4747
pl.col("col2").fill_null((2 * pl.col("col1")).cast(pl.Int64)),
4848
)
49-
print(fill_median_df)
49+
print(fill_expression_df)
5050
# --8<-- [end:fillexpr]
5151

5252
# --8<-- [start:fillstrategy]

docs/source/src/rust/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ required-features = ["polars/lazy"]
7575
[[bin]]
7676
name = "expressions-missing-data"
7777
path = "user-guide/expressions/missing-data.rs"
78-
required-features = ["polars/lazy"]
78+
required-features = ["polars/lazy", "polars/interpolate"]
7979
[[bin]]
8080
name = "expressions-operations"
8181
path = "user-guide/expressions/operations.rs"
8282
required-features = ["polars/lazy", "polars/approx_unique", "polars/dtype-struct", "polars/unique_counts"]
8383
[[bin]]
8484
name = "expressions-strings"
8585
path = "user-guide/expressions/strings.rs"
86-
required-features = ["polars/lazy"]
86+
required-features = ["polars/lazy", "polars/strings", "polars/regex"]
8787
[[bin]]
8888
name = "expressions-structs"
8989
path = "user-guide/expressions/structs.rs"

docs/source/src/rust/user-guide/expressions/aggregation.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3434

3535
let dataset = CsvReadOptions::default()
3636
.with_has_header(true)
37-
.with_schema(Some(Arc::new(schema)))
37+
.with_schema_overwrite(Some(Arc::new(schema)))
3838
.map_parse_options(|parse_options| parse_options.with_try_parse_dates(true))
3939
.into_reader_with_file_handle(Cursor::new(data))
4040
.finish()?;
@@ -88,7 +88,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8888
.clone()
8989
.lazy()
9090
.group_by(["state", "party"])
91-
.agg([len().count().alias("count")])
91+
.agg([len().alias("count")])
9292
.filter(
9393
col("party")
9494
.eq(lit("Anti-Administration"))
@@ -135,7 +135,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
135135
// --8<-- [end:filter]
136136

137137
// --8<-- [start:filter-nested]
138-
// Contribute the Rust translation of the Python example by opening a PR.
138+
let df = dataset
139+
.clone()
140+
.lazy()
141+
.group_by(["state", "gender"])
142+
.agg([compute_age().mean().alias("avg birthday"), len().alias("#")])
143+
.sort(
144+
["#"],
145+
SortMultipleOptions::default()
146+
.with_order_descending(true)
147+
.with_nulls_last(true),
148+
)
149+
.limit(5)
150+
.collect()?;
151+
152+
println!("{}", df);
139153
// --8<-- [end:filter-nested]
140154

141155
// --8<-- [start:sort]

docs/source/src/rust/user-guide/expressions/folds.rs

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,99 @@
11
fn main() -> Result<(), Box<dyn std::error::Error>> {
22
// --8<-- [start:mansum]
3+
use polars::lazy::dsl::sum_horizontal;
34
use polars::prelude::*;
4-
// Contribute the Rust translation of the Python example by opening a PR.
5+
6+
let df = df!(
7+
"label" => ["foo", "bar", "spam"],
8+
"a" => [1, 2, 3],
9+
"b" => [10, 20, 30],
10+
)?;
11+
12+
let result = df
13+
.clone()
14+
.lazy()
15+
.select([
16+
fold_exprs(
17+
lit(0),
18+
|acc, val| (&acc + &val).map(Some),
19+
[col("a"), col("b")],
20+
)
21+
.alias("sum_fold"),
22+
sum_horizontal([col("a"), col("b")], true)?.alias("sum_horz"),
23+
])
24+
.collect()?;
25+
26+
println!("{:?}", result);
527
// --8<-- [end:mansum]
628

729
// --8<-- [start:mansum-explicit]
8-
// Contribute the Rust translation of the Python example by opening a PR.
30+
let acc = lit(0);
31+
let f = |acc: Expr, val: Expr| acc + val;
32+
33+
let result = df
34+
.clone()
35+
.lazy()
36+
.select([
37+
f(f(acc, col("a")), col("b")),
38+
fold_exprs(
39+
lit(0),
40+
|acc, val| (&acc + &val).map(Some),
41+
[col("a"), col("b")],
42+
)
43+
.alias("sum_fold"),
44+
])
45+
.collect()?;
46+
47+
println!("{:?}", result);
948
// --8<-- [end:mansum-explicit]
1049

1150
// --8<-- [start:manprod]
12-
// Contribute the Rust translation of the Python example by opening a PR.
51+
let result = df
52+
.clone()
53+
.lazy()
54+
.select([fold_exprs(
55+
lit(0),
56+
|acc, val| (&acc * &val).map(Some),
57+
[col("a"), col("b")],
58+
)
59+
.alias("prod")])
60+
.collect()?;
61+
62+
println!("{:?}", result);
1363
// --8<-- [end:manprod]
1464

1565
// --8<-- [start:manprod-fixed]
16-
// Contribute the Rust translation of the Python example by opening a PR.
66+
let result = df
67+
.clone()
68+
.lazy()
69+
.select([fold_exprs(
70+
lit(1),
71+
|acc, val| (&acc * &val).map(Some),
72+
[col("a"), col("b")],
73+
)
74+
.alias("prod")])
75+
.collect()?;
76+
77+
println!("{:?}", result);
1778
// --8<-- [end:manprod-fixed]
1879

1980
// --8<-- [start:conditional]
20-
// Contribute the Rust translation of the Python example by opening a PR.
81+
let df = df!(
82+
"a" => [1, 2, 3],
83+
"b" => [0, 1, 2],
84+
)?;
85+
86+
let result = df
87+
.clone()
88+
.lazy()
89+
.filter(fold_exprs(
90+
lit(true),
91+
|acc, val| (&acc & &val).map(Some),
92+
[col("*").gt(1)],
93+
))
94+
.collect()?;
95+
96+
println!("{:?}", result);
2197
// --8<-- [end:conditional]
2298

2399
// --8<-- [start:string]

docs/source/src/rust/user-guide/expressions/missing-data.rs

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,96 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2323
// --8<-- [end:isnull]
2424

2525
// --8<-- [start:dataframe2]
26-
// Contribute the Rust translation of the Python example by opening a PR.
26+
let df = df! (
27+
"col1" => [0.5, 1.0, 1.5, 2.0, 2.5],
28+
"col2" => [Some(1), None, Some(3), None, Some(5)],
29+
)?;
30+
31+
println!("{}", df);
2732
// --8<-- [end:dataframe2]
2833

2934
// --8<-- [start:fill]
30-
// Contribute the Rust translation of the Python example by opening a PR.
35+
let fill_literal_df = df
36+
.clone()
37+
.lazy()
38+
.with_column(col("col2").fill_null(3))
39+
.collect()?;
40+
41+
println!("{}", fill_literal_df);
3142
// --8<-- [end:fill]
3243

3344
// --8<-- [start:fillstrategy]
34-
// Contribute the Rust translation of the Python example by opening a PR.
45+
46+
let fill_literal_df = df
47+
.clone()
48+
.lazy()
49+
.with_columns([
50+
col("col2")
51+
.fill_null_with_strategy(FillNullStrategy::Forward(None))
52+
.alias("forward"),
53+
col("col2")
54+
.fill_null_with_strategy(FillNullStrategy::Backward(None))
55+
.alias("backward"),
56+
])
57+
.collect()?;
58+
59+
println!("{}", fill_literal_df);
3560
// --8<-- [end:fillstrategy]
3661

3762
// --8<-- [start:fillexpr]
38-
// Contribute the Rust translation of the Python example by opening a PR.
63+
let fill_expression_df = df
64+
.clone()
65+
.lazy()
66+
.with_column(col("col2").fill_null((lit(2) * col("col1")).cast(DataType::Int64)))
67+
.collect()?;
68+
69+
println!("{}", fill_expression_df);
3970
// --8<-- [end:fillexpr]
4071

4172
// --8<-- [start:fillinterpolate]
42-
// Contribute the Rust translation of the Python example by opening a PR.
73+
let fill_interpolation_df = df
74+
.clone()
75+
.lazy()
76+
.with_column(col("col2").interpolate(InterpolationMethod::Linear))
77+
.collect()?;
78+
79+
println!("{}", fill_interpolation_df);
4380
// --8<-- [end:fillinterpolate]
4481

4582
// --8<-- [start:nan]
4683
let nan_df = df!(
47-
"value" => [1.0, f64::NAN, f64::NAN, 3.0],
84+
"value" => [1.0, f64::NAN, f64::NAN, 3.0],
4885
)?;
4986
println!("{}", nan_df);
5087
// --8<-- [end:nan]
5188

5289
// --8<-- [start:nan-computed]
53-
// Contribute the Rust translation of the Python example by opening a PR.
90+
let df = df!(
91+
"dividend" => [1.0, 0.0, -1.0],
92+
"divisor" => [1.0, 0.0, -1.0],
93+
)?;
94+
95+
let result = df
96+
.clone()
97+
.lazy()
98+
.select([col("dividend") / col("divisor")])
99+
.collect()?;
100+
101+
println!("{}", result);
54102
// --8<-- [end:nan-computed]
55103

56104
// --8<-- [start:nanfill]
57-
// Contribute the Rust translation of the Python example by opening a PR.
105+
let mean_nan_df = nan_df
106+
.clone()
107+
.lazy()
108+
.with_column(col("value").fill_nan(Null {}.lit()).alias("replaced"))
109+
.select([
110+
col("*").mean().name().suffix("_mean"),
111+
col("*").sum().name().suffix("_sum"),
112+
])
113+
.collect()?;
114+
115+
println!("{}", mean_nan_df);
58116
// --8<-- [end:nanfill]
59117
Ok(())
60118
}

0 commit comments

Comments
 (0)