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/expressions/structs.rs
7889 views
1
fn main() -> Result<(), Box<dyn std::error::Error>> {
2
// --8<-- [start:ratings_df]
3
use polars::prelude::*;
4
let ratings = df!(
5
"Movie"=> ["Cars", "IT", "ET", "Cars", "Up", "IT", "Cars", "ET", "Up", "Cars"],
6
"Theatre"=> ["NE", "ME", "IL", "ND", "NE", "SD", "NE", "IL", "IL", "NE"],
7
"Avg_Rating"=> [4.5, 4.4, 4.6, 4.3, 4.8, 4.7, 4.5, 4.9, 4.7, 4.6],
8
"Count"=> [30, 27, 26, 29, 31, 28, 28, 26, 33, 28],
9
10
)?;
11
println!("{}", &ratings);
12
// --8<-- [end:ratings_df]
13
14
// --8<-- [start:state_value_counts]
15
let result = ratings
16
.clone()
17
.lazy()
18
.select([col("Theatre").value_counts(true, true, "count", false)])
19
.collect()?;
20
println!("{result}");
21
// --8<-- [end:state_value_counts]
22
23
// --8<-- [start:struct_unnest]
24
let result = ratings
25
.clone()
26
.lazy()
27
.select([col("Theatre").value_counts(true, true, "count", false)])
28
.unnest(by_name(["Theatre"], true), None)
29
.collect()?;
30
println!("{result}");
31
// --8<-- [end:struct_unnest]
32
33
// --8<-- [start:series_struct]
34
// Don't think we can make it the same way in rust, but this works
35
let rating_series = df!(
36
"Movie" => &["Cars", "Toy Story"],
37
"Theatre" => &["NE", "ME"],
38
"Avg_Rating" => &[4.5, 4.9],
39
)?
40
.into_struct("ratings".into())
41
.into_series();
42
println!("{}", &rating_series);
43
// // --8<-- [end:series_struct]
44
45
// --8<-- [start:series_struct_error]
46
// Contribute the Rust translation of the Python example by opening a PR.
47
// --8<-- [end:series_struct_error]
48
49
// --8<-- [start:series_struct_extract]
50
let result = rating_series.struct_()?.field_by_name("Movie")?;
51
println!("{result}");
52
// --8<-- [end:series_struct_extract]
53
54
// --8<-- [start:series_struct_rename]
55
// Contribute the Rust translation of the Python example by opening a PR.
56
// --8<-- [end:series_struct_rename]
57
58
// --8<-- [start:struct-rename-check]
59
// Contribute the Rust translation of the Python example by opening a PR.
60
// --8<-- [end:struct-rename-check]
61
62
// --8<-- [start:struct_duplicates]
63
// Contribute the Rust translation of the Python example by opening a PR.
64
// --8<-- [end:struct_duplicates]
65
66
// --8<-- [start:struct_ranking]
67
let result = ratings
68
.lazy()
69
.with_columns([as_struct(vec![col("Count"), col("Avg_Rating")])
70
.rank(
71
RankOptions {
72
method: RankMethod::Dense,
73
descending: true,
74
},
75
None,
76
)
77
.over([col("Movie"), col("Theatre")])
78
.alias("Rank")])
79
// .filter(as_struct(&[col("Movie"), col("Theatre")]).is_duplicated())
80
// Error: .is_duplicated() not available if you try that
81
// https://github.com/pola-rs/polars/issues/3803
82
.filter(len().over([col("Movie"), col("Theatre")]).gt(lit(1)))
83
.collect()?;
84
println!("{result}");
85
// --8<-- [end:struct_ranking]
86
87
// --8<-- [start:multi_column_apply]
88
let df = df!(
89
"keys" => ["a", "a", "b"],
90
"values" => [10, 7, 1],
91
)?;
92
93
let result = df
94
.lazy()
95
.select([
96
// pack to struct to get access to multiple fields in a custom `apply/map`
97
as_struct(vec![col("keys"), col("values")])
98
// we will compute the len(a) + b
99
.apply(
100
|s| {
101
// downcast to struct
102
let ca = s.struct_()?;
103
104
// get the fields as Series
105
let s_a = &ca.fields_as_series()[0];
106
let s_b = &ca.fields_as_series()[1];
107
108
// downcast the `Series` to their known type
109
let ca_a = s_a.str()?;
110
let ca_b = s_b.i32()?;
111
112
// iterate both `ChunkedArrays`
113
let result: Int32Chunked = ca_a
114
.into_iter()
115
.zip(ca_b)
116
.map(|(opt_a, opt_b)| match (opt_a, opt_b) {
117
(Some(a), Some(b)) => Some(a.len() as i32 + b),
118
_ => None,
119
})
120
.collect();
121
122
Ok(result.into_column())
123
},
124
|_, f| Ok(Field::new(f.name().clone(), DataType::Int32)),
125
)
126
// note: the `'solution_map_elements'` alias is just there to show how you
127
// get the same output as in the Python API example.
128
.alias("solution_map_elements"),
129
(col("keys").str().count_matches(lit("."), true) + col("values"))
130
.alias("solution_expr"),
131
])
132
.collect()?;
133
println!("{result}");
134
// --8<-- [end:multi_column_apply]
135
136
// --8<-- [start:ack]
137
// Contribute the Rust translation of the Python example by opening a PR.
138
// --8<-- [end:ack]
139
140
// --8<-- [start:struct-ack]
141
// Contribute the Rust translation of the Python example by opening a PR.
142
// --8<-- [end:struct-ack]
143
144
Ok(())
145
}
146
147