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/syntactic_sugar.rs
6940 views
1
use polars_core::chunked_array::cast::CastOptions;
2
3
use super::*;
4
5
/// Sum all the values in the column named `name`. Shorthand for `col(name).sum()`.
6
pub fn sum(name: &str) -> Expr {
7
col(name).sum()
8
}
9
10
/// Find the minimum of all the values in the column named `name`. Shorthand for `col(name).min()`.
11
pub fn min(name: &str) -> Expr {
12
col(name).min()
13
}
14
15
/// Find the maximum of all the values in the column named `name`. Shorthand for `col(name).max()`.
16
pub fn max(name: &str) -> Expr {
17
col(name).max()
18
}
19
20
/// Find the mean of all the values in the column named `name`. Shorthand for `col(name).mean()`.
21
pub fn mean(name: &str) -> Expr {
22
col(name).mean()
23
}
24
25
/// Find the mean of all the values in the column named `name`. Alias for [`mean`].
26
pub fn avg(name: &str) -> Expr {
27
col(name).mean()
28
}
29
30
/// Find the median of all the values in the column named `name`. Shorthand for `col(name).median()`.
31
pub fn median(name: &str) -> Expr {
32
col(name).median()
33
}
34
35
/// Find a specific quantile of all the values in the column named `name`.
36
pub fn quantile(name: &str, quantile: Expr, method: QuantileMethod) -> Expr {
37
col(name).quantile(quantile, method)
38
}
39
40
/// Negates a boolean column.
41
pub fn not(expr: Expr) -> Expr {
42
expr.not()
43
}
44
45
/// A column which is `true` wherever `expr` is null, `false` elsewhere.
46
pub fn is_null(expr: Expr) -> Expr {
47
expr.is_null()
48
}
49
50
/// A column which is `false` wherever `expr` is null, `true` elsewhere.
51
pub fn is_not_null(expr: Expr) -> Expr {
52
expr.is_not_null()
53
}
54
55
/// Casts the column given by `Expr` to a different type.
56
///
57
/// Follows the rules of Rust casting, with the exception that integers and floats can be cast to `DataType::Date` and
58
/// `DataType::DateTime(_, _)`. A column consisting entirely of `Null` can be cast to any type, regardless of the
59
/// nominal type of the column.
60
pub fn cast(expr: Expr, dtype: impl Into<DataTypeExpr>) -> Expr {
61
Expr::Cast {
62
expr: Arc::new(expr),
63
dtype: dtype.into(),
64
options: CastOptions::NonStrict,
65
}
66
}
67
68