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