Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/dsl/name.rs
8430 views
1
#[cfg(feature = "dtype-struct")]
2
use polars_utils::pl_str::PlSmallStr;
3
4
use super::*;
5
6
/// Specialized expressions for modifying the name of existing expressions.
7
pub struct ExprNameNameSpace(pub(crate) Expr);
8
9
impl ExprNameNameSpace {
10
/// Keep the original root name
11
///
12
/// ```rust,no_run
13
/// # use polars_core::prelude::*;
14
/// # use polars_plan::prelude::*;
15
/// fn example(df: LazyFrame) -> LazyFrame {
16
/// df.select([
17
/// // even thought the alias yields a different column name,
18
/// // `keep` will make sure that the original column name is used
19
/// col("*").alias("foo").name().keep()
20
/// ])
21
/// }
22
/// ```
23
pub fn keep(self) -> Expr {
24
Expr::KeepName(Arc::new(self.0))
25
}
26
27
/// Define an alias by mapping a function over the original root column name.
28
pub fn map(self, function: PlanCallback<PlSmallStr, PlSmallStr>) -> Expr {
29
Expr::RenameAlias {
30
expr: Arc::new(self.0),
31
function: RenameAliasFn::Map(function),
32
}
33
}
34
35
/// Add a prefix to the root column name.
36
pub fn prefix(self, prefix: &str) -> Expr {
37
Expr::RenameAlias {
38
expr: Arc::new(self.0),
39
function: RenameAliasFn::Prefix(prefix.into()),
40
}
41
}
42
43
/// Add a suffix to the root column name.
44
pub fn suffix(self, suffix: &str) -> Expr {
45
Expr::RenameAlias {
46
expr: Arc::new(self.0),
47
function: RenameAliasFn::Suffix(suffix.into()),
48
}
49
}
50
51
/// Replace matching string pattern in the root column name with a new value.
52
#[cfg(feature = "regex")]
53
pub fn replace(self, pattern: &str, value: &str, literal: bool) -> Expr {
54
Expr::RenameAlias {
55
expr: Arc::new(self.0),
56
function: RenameAliasFn::Replace {
57
pattern: pattern.into(),
58
value: value.into(),
59
literal,
60
},
61
}
62
}
63
64
/// Update the root column name to use lowercase characters.
65
#[allow(clippy::wrong_self_convention)]
66
pub fn to_lowercase(self) -> Expr {
67
Expr::RenameAlias {
68
expr: Arc::new(self.0),
69
function: RenameAliasFn::ToLowercase,
70
}
71
}
72
73
/// Update the root column name to use uppercase characters.
74
#[allow(clippy::wrong_self_convention)]
75
pub fn to_uppercase(self) -> Expr {
76
Expr::RenameAlias {
77
expr: Arc::new(self.0),
78
function: RenameAliasFn::ToUppercase,
79
}
80
}
81
82
#[cfg(feature = "dtype-struct")]
83
pub fn map_fields(self, function: PlanCallback<PlSmallStr, PlSmallStr>) -> Expr {
84
self.0
85
.map_unary(FunctionExpr::StructExpr(StructFunction::MapFieldNames(
86
function,
87
)))
88
}
89
90
#[cfg(feature = "dtype-struct")]
91
pub fn prefix_fields(self, prefix: &str) -> Expr {
92
self.0
93
.map_unary(FunctionExpr::StructExpr(StructFunction::PrefixFields(
94
PlSmallStr::from_str(prefix),
95
)))
96
}
97
98
#[cfg(feature = "dtype-struct")]
99
pub fn suffix_fields(self, suffix: &str) -> Expr {
100
self.0
101
.map_unary(FunctionExpr::StructExpr(StructFunction::SuffixFields(
102
PlSmallStr::from_str(suffix),
103
)))
104
}
105
}
106
107
#[cfg(feature = "dtype-struct")]
108
pub type FieldsNameMapper = Arc<dyn Fn(&str) -> PlSmallStr + Send + Sync>;
109
110