Skip to main content

polars_ops/series/ops/
horizontal.rs

1use std::borrow::Cow;
2
3use polars_core::chunked_array::cast::CastOptions;
4use polars_core::prelude::*;
5use polars_core::runtime::RAYON;
6use polars_core::series::arithmetic::coerce_lhs_rhs;
7use polars_core::utils::dtypes_to_supertype;
8use polars_core::with_match_physical_numeric_polars_type;
9use polars_utils::broadcast::broadcast_len;
10use polars_utils::min_max::MinMax;
11use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
12
13pub trait MinMaxHorizontal {
14    /// Aggregate the column horizontally to their min values.
15    fn min_horizontal(&self) -> PolarsResult<Option<Column>>;
16    /// Aggregate the column horizontally to their max values.
17    fn max_horizontal(&self) -> PolarsResult<Option<Column>>;
18}
19
20impl MinMaxHorizontal for DataFrame {
21    fn min_horizontal(&self) -> PolarsResult<Option<Column>> {
22        min_horizontal(self.columns())
23    }
24    fn max_horizontal(&self) -> PolarsResult<Option<Column>> {
25        max_horizontal(self.columns())
26    }
27}
28
29#[derive(Copy, Clone, Debug, PartialEq)]
30pub enum NullStrategy {
31    Ignore,
32    Propagate,
33}
34
35pub trait SumMeanHorizontal {
36    /// Sum all values horizontally across columns.
37    fn sum_horizontal(&self, null_strategy: NullStrategy) -> PolarsResult<Option<Column>>;
38
39    /// Compute the mean of all numeric values horizontally across columns.
40    fn mean_horizontal(&self, null_strategy: NullStrategy) -> PolarsResult<Option<Column>>;
41}
42
43impl SumMeanHorizontal for DataFrame {
44    fn sum_horizontal(&self, null_strategy: NullStrategy) -> PolarsResult<Option<Column>> {
45        sum_horizontal(self.columns(), null_strategy)
46    }
47    fn mean_horizontal(&self, null_strategy: NullStrategy) -> PolarsResult<Option<Column>> {
48        mean_horizontal(self.columns(), null_strategy)
49    }
50}
51
52fn min_binary<T>(left: &ChunkedArray<T>, right: &ChunkedArray<T>) -> ChunkedArray<T>
53where
54    T: PolarsNumericType,
55    T::Native: MinMax,
56{
57    if !left.has_nulls() && !right.has_nulls() {
58        arity::broadcast_binary_elementwise_values(left, right, MinMax::min_ignore_nan)
59    } else {
60        arity::broadcast_binary_elementwise(left, right, |opt_l, opt_r| match (opt_l, opt_r) {
61            (Some(l), Some(r)) => Some(l.min_ignore_nan(r)),
62            (Some(x), None) | (None, Some(x)) => Some(x),
63            (None, None) => None,
64        })
65    }
66}
67
68fn max_binary<T>(left: &ChunkedArray<T>, right: &ChunkedArray<T>) -> ChunkedArray<T>
69where
70    T: PolarsNumericType,
71    T::Native: MinMax,
72{
73    if !left.has_nulls() && !right.has_nulls() {
74        arity::broadcast_binary_elementwise_values(left, right, MinMax::max_ignore_nan)
75    } else {
76        arity::broadcast_binary_elementwise(left, right, |opt_l, opt_r| match (opt_l, opt_r) {
77            (Some(l), Some(r)) => Some(l.max_ignore_nan(r)),
78            (Some(x), None) | (None, Some(x)) => Some(x),
79            (None, None) => None,
80        })
81    }
82}
83
84fn min_max_binary_columns(left: &Column, right: &Column, min: bool) -> PolarsResult<Column> {
85    if left.dtype().to_physical().is_primitive_numeric()
86        && right.dtype().to_physical().is_primitive_numeric()
87    {
88        let height = broadcast_len([left, right])?;
89        let left_s = left.as_materialized_series_maintain_scalar();
90        let right_s = right.as_materialized_series_maintain_scalar();
91        let (lhs, rhs) = coerce_lhs_rhs(&left_s, &right_s)?;
92        let logical = lhs.dtype();
93
94        let lhs = lhs.to_physical_repr();
95        let rhs = rhs.to_physical_repr();
96
97        with_match_physical_numeric_polars_type!(lhs.dtype(), |$T| {
98            let a: &ChunkedArray<$T> = lhs.as_ref().as_ref().as_ref();
99            let b: &ChunkedArray<$T> = rhs.as_ref().as_ref().as_ref();
100
101            unsafe {
102                if min {
103                    min_binary(a, b).into_series().from_physical_unchecked(logical)
104                } else {
105                    max_binary(a, b).into_series().from_physical_unchecked(logical)
106                }
107            }
108        })
109        .and_then(|s| s.broadcast_owned_to(height))
110        .map(Column::from)
111    } else {
112        let mut mask = if min {
113            left.lt(right)?
114        } else {
115            left.gt(right)?
116        };
117        if left.has_nulls() {
118            mask = mask & left.is_not_null();
119        }
120        if right.has_nulls() {
121            mask = mask | right.is_null();
122        }
123        left.zip_with(&mask, right)
124    }
125}
126
127pub fn max_horizontal(columns: &[Column]) -> PolarsResult<Option<Column>> {
128    broadcast_len(columns.iter()).context("max_horizontal")?;
129
130    let max_fn = |acc: &Column, s: &Column| min_max_binary_columns(acc, s, false);
131
132    match columns.len() {
133        0 => Ok(None),
134        1 => Ok(Some(columns[0].clone())),
135        2 => max_fn(&columns[0], &columns[1]).map(Some),
136        _ => {
137            // the try_reduce_with is a bit slower in parallelism,
138            // but I don't think it matters here as we parallelize over columns, not over elements
139            RAYON.install(|| {
140                columns
141                    .par_iter()
142                    .map(|s| Ok(Cow::Borrowed(s)))
143                    .try_reduce_with(|l, r| max_fn(&l, &r).map(Cow::Owned))
144                    // we can unwrap the option, because we are certain there is a column
145                    // we started this operation on 3 columns
146                    .unwrap()
147                    .map(|cow| Some(cow.into_owned()))
148            })
149        },
150    }
151}
152
153pub fn min_horizontal(columns: &[Column]) -> PolarsResult<Option<Column>> {
154    broadcast_len(columns.iter()).context("min_horizontal")?;
155
156    let min_fn = |acc: &Column, s: &Column| min_max_binary_columns(acc, s, true);
157
158    match columns.len() {
159        0 => Ok(None),
160        1 => Ok(Some(columns[0].clone())),
161        2 => min_fn(&columns[0], &columns[1]).map(Some),
162        _ => {
163            // the try_reduce_with is a bit slower in parallelism,
164            // but I don't think it matters here as we parallelize over columns, not over elements
165            RAYON.install(|| {
166                columns
167                    .par_iter()
168                    .map(|s| Ok(Cow::Borrowed(s)))
169                    .try_reduce_with(|l, r| min_fn(&l, &r).map(Cow::Owned))
170                    // we can unwrap the option, because we are certain there is a column
171                    // we started this operation on 3 columns
172                    .unwrap()
173                    .map(|cow| Some(cow.into_owned()))
174            })
175        },
176    }
177}
178
179pub fn sum_horizontal(
180    columns: &[Column],
181    null_strategy: NullStrategy,
182) -> PolarsResult<Option<Column>> {
183    broadcast_len(columns.iter()).context("sum_horizontal")?;
184    let ignore_nulls = null_strategy == NullStrategy::Ignore;
185
186    let apply_null_strategy = |s: Series| -> PolarsResult<Series> {
187        if ignore_nulls && s.null_count() > 0 {
188            s.fill_null(FillNullStrategy::Zero)
189        } else {
190            Ok(s)
191        }
192    };
193
194    let sum_fn = |acc: Series, s: Series| -> PolarsResult<Series> {
195        let acc: Series = apply_null_strategy(acc)?;
196        let s = apply_null_strategy(s)?;
197        // This will do owned arithmetic and can be mutable
198        std::ops::Add::add(acc, s)
199    };
200
201    // @scalar-opt
202    let non_null_cols = columns
203        .iter()
204        .filter(|x| x.dtype() != &DataType::Null)
205        .map(|c| c.as_materialized_series())
206        .collect::<Vec<_>>();
207
208    // If we have any null columns and null strategy is not `Ignore`, we can return immediately.
209    if !ignore_nulls && non_null_cols.len() < columns.len() {
210        // We must determine the correct return dtype.
211        let return_dtype = match dtypes_to_supertype(non_null_cols.iter().map(|c| c.dtype()))? {
212            DataType::Boolean => IDX_DTYPE,
213            dt => dt,
214        };
215        return Ok(Some(Column::full_null(
216            columns[0].name().clone(),
217            columns[0].len(),
218            &return_dtype,
219        )));
220    }
221
222    match non_null_cols.len() {
223        0 => {
224            if columns.is_empty() {
225                Ok(None)
226            } else {
227                // all columns are null dtype, so result is null dtype
228                Ok(Some(columns[0].clone()))
229            }
230        },
231        1 => Ok(Some(
232            apply_null_strategy(if non_null_cols[0].dtype() == &DataType::Boolean {
233                non_null_cols[0].cast(&IDX_DTYPE)?
234            } else {
235                non_null_cols[0].clone()
236            })?
237            .into(),
238        )),
239        2 => sum_fn(non_null_cols[0].clone(), non_null_cols[1].clone())
240            .map(Column::from)
241            .map(Some),
242        _ => {
243            // the try_reduce_with is a bit slower in parallelism,
244            // but I don't think it matters here as we parallelize over columns, not over elements
245            let out = RAYON.install(|| {
246                non_null_cols
247                    .into_par_iter()
248                    .cloned()
249                    .map(Ok)
250                    .try_reduce_with(sum_fn)
251                    // We can unwrap because we started with at least 3 columns, so we always get a Some
252                    .unwrap()
253            });
254            out.map(Column::from).map(Some)
255        },
256    }
257}
258
259pub fn mean_horizontal(
260    columns: &[Column],
261    null_strategy: NullStrategy,
262) -> PolarsResult<Option<Column>> {
263    broadcast_len(columns.iter()).context("mean_horizontal")?;
264
265    let (numeric_columns, non_numeric_columns): (Vec<_>, Vec<_>) = columns.iter().partition(|s| {
266        let dtype = s.dtype();
267        dtype.is_primitive_numeric() || dtype.is_decimal() || dtype.is_bool() || dtype.is_null()
268    });
269
270    if !non_numeric_columns.is_empty() {
271        let col = non_numeric_columns.first().cloned();
272        polars_bail!(
273            InvalidOperation: "'horizontal_mean' expects numeric expressions, found {:?} (dtype={})",
274            col.unwrap().name(),
275            col.unwrap().dtype(),
276        );
277    }
278    let columns = numeric_columns.into_iter().cloned().collect::<Vec<_>>();
279    let num_rows = columns.len();
280    match num_rows {
281        0 => Ok(None),
282        1 => Ok(Some(match columns[0].dtype() {
283            dt if !matches!(dt, DataType::Float16 | DataType::Float32) && !dt.is_decimal() => {
284                columns[0].cast(&DataType::Float64)?
285            },
286            _ => columns[0].clone(),
287        })),
288        _ => {
289            let sum = || sum_horizontal(columns.as_slice(), null_strategy);
290            let null_count = || {
291                columns
292                    .par_iter()
293                    .map(|c| {
294                        c.is_null()
295                            .into_column()
296                            .cast_with_options(&DataType::UInt32, CastOptions::NonStrict)
297                    })
298                    .reduce_with(|l, r| {
299                        let l = l?;
300                        let r = r?;
301                        let result = std::ops::Add::add(&l, &r)?;
302                        PolarsResult::Ok(result)
303                    })
304                    // we can unwrap the option, because we are certain there is a column
305                    // we started this operation on 2 columns
306                    .unwrap()
307            };
308
309            let (sum, null_count) = RAYON.install(|| rayon::join(sum, null_count));
310            let sum = sum?;
311            let null_count = null_count?;
312
313            // value lengths: len - null_count
314            let value_length: UInt32Chunked = (Column::new_scalar(
315                PlSmallStr::EMPTY,
316                Scalar::from(num_rows as u32),
317                null_count.len(),
318            ) - null_count)?
319                .u32()
320                .unwrap()
321                .clone();
322
323            // make sure that we do not divide by zero
324            // by replacing with None
325            let dt = sum
326                .as_ref()
327                .map(Column::dtype)
328                .filter(|dt| matches!(dt, DataType::Float16 | DataType::Float32))
329                .unwrap_or(&DataType::Float64);
330            let value_length = value_length
331                .set(&value_length.equal(0), None)?
332                .into_column()
333                .cast(dt)?;
334
335            sum.map(|sum| std::ops::Div::div(&sum, &value_length))
336                .transpose()
337        },
338    }
339}
340
341pub fn coalesce_columns(s: &[Column]) -> PolarsResult<Column> {
342    // TODO! this can be faster if we have more than two inputs.
343    polars_ensure!(!s.is_empty(), NoData: "cannot coalesce empty list");
344    let mut out = s[0].clone();
345    for s in s {
346        if !out.null_count() == 0 {
347            return Ok(out);
348        } else {
349            let mask = out.is_not_null();
350            out = out
351                .as_materialized_series()
352                .zip_with_same_type(&mask, s.as_materialized_series())?
353                .into();
354        }
355    }
356    Ok(out)
357}
358
359#[cfg(test)]
360mod tests {
361    use super::*;
362
363    #[test]
364    #[cfg_attr(miri, ignore)]
365    fn test_horizontal_agg() {
366        let a = Column::new("a".into(), [1, 2, 6]);
367        let b = Column::new("b".into(), [Some(1), None, None]);
368        let c = Column::new("c".into(), [Some(4), None, Some(3)]);
369
370        let df = DataFrame::new_infer_height(vec![a, b, c]).unwrap();
371        assert_eq!(
372            Vec::from(
373                df.mean_horizontal(NullStrategy::Ignore)
374                    .unwrap()
375                    .unwrap()
376                    .f64()
377                    .unwrap()
378            ),
379            &[Some(2.0), Some(2.0), Some(4.5)]
380        );
381        assert_eq!(
382            Vec::from(
383                df.sum_horizontal(NullStrategy::Ignore)
384                    .unwrap()
385                    .unwrap()
386                    .i32()
387                    .unwrap()
388            ),
389            &[Some(6), Some(2), Some(9)]
390        );
391        assert_eq!(
392            Vec::from(df.min_horizontal().unwrap().unwrap().i32().unwrap()),
393            &[Some(1), Some(2), Some(3)]
394        );
395        assert_eq!(
396            Vec::from(df.max_horizontal().unwrap().unwrap().i32().unwrap()),
397            &[Some(4), Some(2), Some(6)]
398        );
399    }
400}