Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/dsl/functions/selectors.rs
8431 views
1
use super::*;
2
3
/// Create a Column Expression based on a column name.
4
///
5
/// # Arguments
6
///
7
/// * `name` - A string slice that holds the name of the column. If a column with this name does not exist when the
8
/// LazyFrame is collected, an error is returned.
9
///
10
/// # Examples
11
///
12
/// ```ignore
13
/// // select a column name
14
/// col("foo")
15
/// ```
16
///
17
/// ```ignore
18
/// // select all columns by using a wildcard
19
/// col("*")
20
/// ```
21
///
22
/// ```ignore
23
/// // select specific columns by writing a regular expression that starts with `^` and ends with `$`
24
/// // only if regex features is activated
25
/// col("^foo.*$")
26
/// ```
27
pub fn col<S>(name: S) -> Expr
28
where
29
S: Into<PlSmallStr>,
30
{
31
let name = name.into();
32
match name.as_str() {
33
"*" => all().as_expr(),
34
n if is_regex_projection(n) => Expr::Selector(Selector::Matches(name)),
35
_ => Expr::Column(name),
36
}
37
}
38
39
pub fn element() -> Expr {
40
Expr::Element
41
}
42
43
/// Selects no columns.
44
pub fn empty() -> Selector {
45
Selector::Empty
46
}
47
48
/// Selects all columns.
49
pub fn all() -> Selector {
50
Selector::Wildcard
51
}
52
53
/// Select multiple columns by name.
54
pub fn cols<I, S>(names: I) -> Selector
55
where
56
I: IntoIterator<Item = S>,
57
S: Into<PlSmallStr>,
58
{
59
by_name(names, true, true)
60
}
61
62
/// Select multiple columns by dtype.
63
pub fn dtype_col(dtype: &DataType) -> DataTypeSelector {
64
DataTypeSelector::AnyOf([dtype.clone()].into())
65
}
66
67
/// Select multiple columns by dtype.
68
pub fn dtype_cols<DT: AsRef<[DataType]>>(dtype: DT) -> DataTypeSelector {
69
let dtypes = dtype.as_ref();
70
DataTypeSelector::AnyOf(dtypes.into())
71
}
72
73
/// Select multiple columns by name.
74
///
75
/// When `expand_patterns` is `true`, a single wildcard `"*"` and anchored regex patterns
76
/// (e.g. `"^...$"`) are expanded to their matching columns. When `false`, names are
77
/// treated as literals.
78
pub fn by_name<S: Into<PlSmallStr>, I: IntoIterator<Item = S>>(
79
names: I,
80
strict: bool,
81
expand_patterns: bool,
82
) -> Selector {
83
if !expand_patterns {
84
let names = names.into_iter().map(Into::into).collect::<Arc<[_]>>();
85
return Selector::ByName { names, strict };
86
}
87
88
// When expand_patterns is true, handle wildcards and regex patterns
89
let mut selector = None;
90
let _s = &mut selector;
91
let names = names
92
.into_iter()
93
.map(Into::into)
94
.filter_map(|name| match name.as_str() {
95
"*" => {
96
*_s = Some(std::mem::take(_s).map_or(all(), |s| s | all()));
97
None
98
},
99
n if is_regex_projection(n) => {
100
let m = Selector::Matches(name);
101
*_s = Some(std::mem::take(_s).map_or_else(|| m.clone(), |s| s | m.clone()));
102
None
103
},
104
_ => Some(name),
105
})
106
.collect::<Arc<[_]>>();
107
108
let no_names = names.is_empty();
109
let names = Selector::ByName { names, strict };
110
if let Some(selector) = selector {
111
if no_names { selector } else { selector | names }
112
} else {
113
names
114
}
115
}
116
117
/// Select multiple columns by index.
118
pub fn index_cols<N: AsRef<[i64]>>(indices: N) -> Selector {
119
let indices = indices.as_ref().into();
120
Selector::ByIndex {
121
indices,
122
strict: true,
123
}
124
}
125
126