Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/docs/source/src/rust/user-guide/concepts/expressions.rs
7889 views
1
fn main() -> Result<(), Box<dyn std::error::Error>> {
2
// --8<-- [start:df]
3
use chrono::prelude::*;
4
use polars::prelude::*;
5
6
let df: DataFrame = df!(
7
"name" => ["Alice Archer", "Ben Brown", "Chloe Cooper", "Daniel Donovan"],
8
"birthdate" => [
9
NaiveDate::from_ymd_opt(1997, 1, 10).unwrap(),
10
NaiveDate::from_ymd_opt(1985, 2, 15).unwrap(),
11
NaiveDate::from_ymd_opt(1983, 3, 22).unwrap(),
12
NaiveDate::from_ymd_opt(1981, 4, 30).unwrap(),
13
],
14
"weight" => [57.9, 72.5, 53.6, 83.1], // (kg)
15
"height" => [1.56, 1.77, 1.65, 1.75], // (m)
16
)
17
.unwrap();
18
println!("{df}");
19
// --8<-- [end:df]
20
21
// --8<-- [start:select-1]
22
let bmi = col("weight") / col("height").pow(2);
23
let result = df
24
.clone()
25
.lazy()
26
.select([
27
bmi.clone().alias("bmi"),
28
bmi.clone().mean().alias("avg_bmi"),
29
lit(25).alias("ideal_max_bmi"),
30
])
31
.collect()?;
32
println!("{result}");
33
// --8<-- [end:select-1]
34
35
// --8<-- [start:select-2]
36
let result = df
37
.clone()
38
.lazy()
39
.select([((bmi.clone() - bmi.clone().mean()) / bmi.clone().std(1)).alias("deviation")])
40
.collect()?;
41
println!("{result}");
42
// --8<-- [end:select-2]
43
44
// --8<-- [start:with_columns-1]
45
let result = df
46
.clone()
47
.lazy()
48
.with_columns([
49
bmi.clone().alias("bmi"),
50
bmi.mean().alias("avg_bmi"),
51
lit(25).alias("ideal_max_bmi"),
52
])
53
.collect()?;
54
println!("{result}");
55
// --8<-- [end:with_columns-1]
56
57
// --8<-- [start:filter-1]
58
let result = df
59
.clone()
60
.lazy()
61
.filter(
62
col("birthdate")
63
.is_between(
64
lit(NaiveDate::from_ymd_opt(1982, 12, 31).unwrap()),
65
lit(NaiveDate::from_ymd_opt(1996, 1, 1).unwrap()),
66
ClosedInterval::Both,
67
)
68
.and(col("height").gt(lit(1.7))),
69
)
70
.collect()?;
71
println!("{result}");
72
// --8<-- [end:filter-1]
73
74
// --8<-- [start:group_by-1]
75
let result = df
76
.clone()
77
.lazy()
78
.group_by([(col("birthdate").dt().year() / lit(10) * lit(10)).alias("decade")])
79
.agg([col("name")])
80
.collect()?;
81
println!("{result}");
82
// --8<-- [end:group_by-1]
83
84
// --8<-- [start:group_by-2]
85
let result = df
86
.clone()
87
.lazy()
88
.group_by([
89
(col("birthdate").dt().year() / lit(10) * lit(10)).alias("decade"),
90
(col("height").lt(lit(1.7)).alias("short?")),
91
])
92
.agg([col("name")])
93
.collect()?;
94
println!("{result}");
95
// --8<-- [end:group_by-2]
96
97
// --8<-- [start:group_by-3]
98
let result = df
99
.clone()
100
.lazy()
101
.group_by([
102
(col("birthdate").dt().year() / lit(10) * lit(10)).alias("decade"),
103
(col("height").lt(lit(1.7)).alias("short?")),
104
])
105
.agg([
106
len(),
107
col("height").max().alias("tallest"),
108
cols(["weight", "height"])
109
.as_expr()
110
.mean()
111
.name()
112
.prefix("avg_"),
113
])
114
.collect()?;
115
println!("{result}");
116
// --8<-- [end:group_by-3]
117
118
// --8<-- [start:expression-expansion-1]
119
let expr = (dtype_col(&DataType::Float64).as_selector().as_expr() * lit(1.1))
120
.name()
121
.suffix("*1.1");
122
let result = df.lazy().select([expr.clone()]).collect()?;
123
println!("{result}");
124
// --8<-- [end:expression-expansion-1]
125
126
// --8<-- [start:expression-expansion-2]
127
let df2: DataFrame = df!(
128
"ints" => [1, 2, 3, 4],
129
"letters" => ["A", "B", "C", "D"],
130
)
131
.unwrap();
132
let result = df2.lazy().select([expr]).collect()?;
133
println!("{result}");
134
// --8<-- [end:expression-expansion-2]
135
136
Ok(())
137
}
138
139