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/horizontal.rs
6940 views
1
use super::*;
2
3
/// Accumulate over multiple columns horizontally / row wise.
4
pub fn fold_exprs<E>(
5
acc: Expr,
6
f: PlanCallback<(Series, Series), Series>,
7
exprs: E,
8
returns_scalar: bool,
9
return_dtype: Option<DataTypeExpr>,
10
) -> Expr
11
where
12
E: AsRef<[Expr]>,
13
{
14
let mut exprs_v = Vec::with_capacity(exprs.as_ref().len() + 1);
15
exprs_v.push(acc);
16
exprs_v.extend(exprs.as_ref().iter().cloned());
17
18
Expr::Function {
19
input: exprs_v,
20
function: FunctionExpr::FoldHorizontal {
21
callback: f,
22
returns_scalar,
23
return_dtype,
24
},
25
}
26
}
27
28
/// Analogous to [`Iterator::reduce`](std::iter::Iterator::reduce).
29
///
30
/// An accumulator is initialized to the series given by the first expression in `exprs`, and then each subsequent value
31
/// of the accumulator is computed from `f(acc, next_expr_series)`. If `exprs` is empty, an error is returned when
32
/// `collect` is called.
33
pub fn reduce_exprs<E>(
34
f: PlanCallback<(Series, Series), Series>,
35
exprs: E,
36
returns_scalar: bool,
37
return_dtype: Option<DataTypeExpr>,
38
) -> Expr
39
where
40
E: AsRef<[Expr]>,
41
{
42
let exprs = exprs.as_ref().to_vec();
43
44
Expr::Function {
45
input: exprs,
46
function: FunctionExpr::ReduceHorizontal {
47
callback: f,
48
returns_scalar,
49
return_dtype,
50
},
51
}
52
}
53
54
/// Accumulate over multiple columns horizontally / row wise.
55
#[cfg(feature = "dtype-struct")]
56
pub fn cum_reduce_exprs<E>(
57
f: PlanCallback<(Series, Series), Series>,
58
exprs: E,
59
60
returns_scalar: bool,
61
return_dtype: Option<DataTypeExpr>,
62
) -> Expr
63
where
64
E: AsRef<[Expr]>,
65
{
66
let exprs = exprs.as_ref().to_vec();
67
68
Expr::Function {
69
input: exprs,
70
function: FunctionExpr::CumReduceHorizontal {
71
callback: f,
72
returns_scalar,
73
return_dtype,
74
},
75
}
76
}
77
78
/// Accumulate over multiple columns horizontally / row wise.
79
#[cfg(feature = "dtype-struct")]
80
pub fn cum_fold_exprs<E>(
81
acc: Expr,
82
f: PlanCallback<(Series, Series), Series>,
83
exprs: E,
84
returns_scalar: bool,
85
return_dtype: Option<DataTypeExpr>,
86
include_init: bool,
87
) -> Expr
88
where
89
E: AsRef<[Expr]>,
90
{
91
let exprs = exprs.as_ref();
92
let mut exprs_v = Vec::with_capacity(exprs.len());
93
exprs_v.push(acc);
94
exprs_v.extend(exprs.iter().cloned());
95
96
Expr::Function {
97
input: exprs_v,
98
function: FunctionExpr::CumFoldHorizontal {
99
callback: f,
100
returns_scalar,
101
return_dtype,
102
include_init,
103
},
104
}
105
}
106
107
/// Create a new column with the bitwise-and of the elements in each row.
108
///
109
/// The name of the resulting column will be "all"; use [`alias`](Expr::alias) to choose a different name.
110
pub fn all_horizontal<E: AsRef<[Expr]>>(exprs: E) -> PolarsResult<Expr> {
111
let exprs = exprs.as_ref().to_vec();
112
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
113
// This will be reduced to `expr & expr` during conversion to IR.
114
Ok(Expr::n_ary(
115
FunctionExpr::Boolean(BooleanFunction::AllHorizontal),
116
exprs,
117
))
118
}
119
120
/// Create a new column with the bitwise-or of the elements in each row.
121
///
122
/// The name of the resulting column will be "any"; use [`alias`](Expr::alias) to choose a different name.
123
pub fn any_horizontal<E: AsRef<[Expr]>>(exprs: E) -> PolarsResult<Expr> {
124
let exprs = exprs.as_ref().to_vec();
125
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
126
// This will be reduced to `expr | expr` during conversion to IR.
127
Ok(Expr::n_ary(
128
FunctionExpr::Boolean(BooleanFunction::AnyHorizontal),
129
exprs,
130
))
131
}
132
133
/// Create a new column with the maximum value per row.
134
///
135
/// The name of the resulting column will be `"max"`; use [`alias`](Expr::alias) to choose a different name.
136
pub fn max_horizontal<E: AsRef<[Expr]>>(exprs: E) -> PolarsResult<Expr> {
137
let exprs = exprs.as_ref().to_vec();
138
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
139
Ok(Expr::n_ary(FunctionExpr::MaxHorizontal, exprs))
140
}
141
142
/// Create a new column with the minimum value per row.
143
///
144
/// The name of the resulting column will be `"min"`; use [`alias`](Expr::alias) to choose a different name.
145
pub fn min_horizontal<E: AsRef<[Expr]>>(exprs: E) -> PolarsResult<Expr> {
146
let exprs = exprs.as_ref().to_vec();
147
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
148
Ok(Expr::n_ary(FunctionExpr::MinHorizontal, exprs))
149
}
150
151
/// Sum all values horizontally across columns.
152
pub fn sum_horizontal<E: AsRef<[Expr]>>(exprs: E, ignore_nulls: bool) -> PolarsResult<Expr> {
153
let exprs = exprs.as_ref().to_vec();
154
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
155
Ok(Expr::n_ary(
156
FunctionExpr::SumHorizontal { ignore_nulls },
157
exprs,
158
))
159
}
160
161
/// Compute the mean of all values horizontally across columns.
162
pub fn mean_horizontal<E: AsRef<[Expr]>>(exprs: E, ignore_nulls: bool) -> PolarsResult<Expr> {
163
let exprs = exprs.as_ref().to_vec();
164
polars_ensure!(!exprs.is_empty(), ComputeError: "cannot return empty fold because the number of output rows is unknown");
165
Ok(Expr::n_ary(
166
FunctionExpr::MeanHorizontal { ignore_nulls },
167
exprs,
168
))
169
}
170
171
/// Folds the expressions from left to right keeping the first non-null values.
172
///
173
/// It is an error to provide an empty `exprs`.
174
pub fn coalesce(exprs: &[Expr]) -> Expr {
175
Expr::n_ary(FunctionExpr::Coalesce, exprs.to_vec())
176
}
177
178