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/column-selections.rs
7889 views
1
use polars::prelude::*;
2
3
fn main() -> Result<(), Box<dyn std::error::Error>> {
4
// --8<-- [start:selectors_df]
5
6
use chrono::prelude::*;
7
use polars::time::*;
8
9
let df = df!(
10
"id" => &[9, 4, 2],
11
"place" => &["Mars", "Earth", "Saturn"],
12
"date" => date_range("date".into(),
13
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)?,
14
"sales" => &[33.4, 2142134.1, 44.7],
15
"has_people" => &[false, true, false],
16
"logged_at" => date_range("logged_at".into(),
17
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)?,
18
)?
19
.with_row_index("index".into(), None)?;
20
println!("{}", &df);
21
// --8<-- [end:selectors_df]
22
23
// --8<-- [start:all]
24
let out = df.clone().lazy().select([col("*")]).collect()?;
25
println!("{}", &out);
26
27
// Is equivalent to
28
let out = df.clone().lazy().select([all().as_expr()]).collect()?;
29
println!("{}", &out);
30
// --8<-- [end:all]
31
32
// --8<-- [start:exclude]
33
let out = df
34
.clone()
35
.lazy()
36
.select([all().exclude_cols(["logged_at", "index"]).as_expr()])
37
.collect()?;
38
println!("{}", &out);
39
// --8<-- [end:exclude]
40
41
// --8<-- [start:expansion_by_names]
42
let out = df
43
.clone()
44
.lazy()
45
.select([cols(["date", "logged_at"])
46
.as_expr()
47
.dt()
48
.to_string("%Y-%h-%d")])
49
.collect()?;
50
println!("{}", &out);
51
// --8<-- [end:expansion_by_names]
52
53
// --8<-- [start:expansion_by_regex]
54
let out = df.clone().lazy().select([col("^.*(as|sa).*$")]).collect()?;
55
println!("{}", &out);
56
// --8<-- [end:expansion_by_regex]
57
58
// --8<-- [start:expansion_by_dtype]
59
let out = df
60
.lazy()
61
.select([
62
dtype_cols([DataType::Int64, DataType::UInt32, DataType::Boolean])
63
.as_selector()
64
.as_expr()
65
.n_unique(),
66
])
67
.collect()?;
68
// gives different result than python as the id col is i32 in rust
69
println!("{}", &out);
70
// --8<-- [end:expansion_by_dtype]
71
72
// --8<-- [start:selectors_intro]
73
// Not available in Rust, refer the following link
74
// https://github.com/pola-rs/polars/issues/10594
75
// --8<-- [end:selectors_intro]
76
77
// --8<-- [start:selectors_diff]
78
// Not available in Rust, refer the following link
79
// https://github.com/pola-rs/polars/issues/10594
80
// --8<-- [end:selectors_diff]
81
82
// --8<-- [start:selectors_union]
83
// Not available in Rust, refer the following link
84
// https://github.com/pola-rs/polars/issues/10594
85
// --8<-- [end:selectors_union]
86
87
// --8<-- [start:selectors_by_name]
88
// Not available in Rust, refer the following link
89
// https://github.com/pola-rs/polars/issues/10594
90
// --8<-- [end:selectors_by_name]
91
92
// --8<-- [start:selectors_to_expr]
93
// Not available in Rust, refer the following link
94
// https://github.com/pola-rs/polars/issues/10594
95
// --8<-- [end:selectors_to_expr]
96
97
// --8<-- [start:selectors_is_selector_utility]
98
// Not available in Rust, refer the following link
99
// https://github.com/pola-rs/polars/issues/10594
100
// --8<-- [end:selectors_is_selector_utility]
101
102
// --8<-- [start:selectors_colnames_utility]
103
// Not available in Rust, refer the following link
104
// https://github.com/pola-rs/polars/issues/10594
105
// --8<-- [end:selectors_colnames_utility]
106
Ok(())
107
}
108
109