Skip to main content

polars_core/frame/group_by/aggregations/
mod.rs

1mod agg_list;
2mod boolean;
3#[cfg(feature = "dtype-categorical")]
4mod categorical;
5mod dispatch;
6mod string;
7
8use std::borrow::Cow;
9
10pub use agg_list::*;
11pub(crate) use dispatch::nulls_at_one_end;
12use num_traits::pow::Pow;
13use num_traits::{Bounded, Float, Num, NumCast, ToPrimitive, Zero};
14use polars_arrow::bitmap::{Bitmap, MutableBitmap};
15use polars_arrow::legacy::kernels::take_agg::*;
16use polars_arrow::legacy::trusted_len::TrustedLenPush;
17use polars_arrow::types::NativeType;
18use polars_compute::rolling::no_nulls::{MomentWindow, QuantileWindow, RollingAggWindowNoNulls};
19use polars_compute::rolling::nulls::{RollingAggWindowNulls, VarianceMoment};
20use polars_compute::rolling::{
21    self, ArgMinMaxWindow, MeanWindow, MinMaxWindow, QuantileMethod, RollingFnParams,
22    RollingQuantileParams, RollingVarParams, SumWindow, quantile_filter, rolling_argmax_by,
23    rolling_argmin_by,
24};
25use polars_utils::arg_min_max::ArgMinMax;
26use polars_utils::float::IsFloat;
27use polars_utils::idx_vec::IdxVec;
28use polars_utils::kahan_sum::KahanSum;
29use polars_utils::min_max::{MaxIgnoreNan, MinIgnoreNan, MinMax};
30use polars_utils::scratch_vec::ScratchVec;
31use rayon::prelude::*;
32
33use crate::chunked_array::cast::CastOptions;
34use crate::chunked_array::from_iterator_par::{
35    collect_primitive_opt_par, collect_primitive_opt_par_with_scratch,
36};
37#[cfg(feature = "object")]
38use crate::chunked_array::object::extension::create_extension;
39use crate::chunked_array::ops::aggregate::{quantile_of_nonnull, quantile_with, sorted_quantile};
40use crate::chunked_array::{arg_max_numeric, arg_min_numeric};
41#[cfg(feature = "object")]
42use crate::frame::group_by::GroupsIndicator;
43use crate::prelude::*;
44use crate::runtime::RAYON;
45use crate::series::IsSorted;
46use crate::series::implementations::SeriesWrap;
47use crate::utils::{Container, NoNull};
48
49#[inline]
50fn idx2usize(idx: &[IdxSize]) -> impl ExactSizeIterator<Item = usize> + '_ {
51    idx.iter().map(|i| *i as usize)
52}
53
54/// Whether every index in `idx` is below `len`.
55///
56/// A group can repeat an index, so `idx.len()` can exceed `len`.
57#[inline]
58pub fn _idx_in_bounds(idx: &[IdxSize], len: usize) -> bool {
59    idx.iter().all(|&i| (i as usize) < len)
60}
61
62// Unlike `rolling_min` and `rolling_max`, group aggregations ignore NaN.
63type MinWindow<'a, T> = MinMaxWindow<'a, T, MinIgnoreNan>;
64type MaxWindow<'a, T> = MinMaxWindow<'a, T, MaxIgnoreNan>;
65type ArgMinWindow<'a, T> = ArgMinMaxWindow<'a, T, MinIgnoreNan>;
66type ArgMaxWindow<'a, T> = ArgMinMaxWindow<'a, T, MaxIgnoreNan>;
67
68// if the windows overlap, we can use the rolling_<agg> kernels
69// they maintain state, which saves a lot of compute by not naively traversing all elements every
70// window
71//
72// if the windows don't overlap, we should not use these kernels as they are single threaded, so
73// we miss out on easy parallelization.
74pub fn _use_rolling_kernels(
75    groups: &GroupsSlice,
76    overlapping: bool,
77    monotonic: bool,
78    chunks: &[ArrayRef],
79) -> bool {
80    match groups.len() {
81        0 | 1 => false,
82        _ => overlapping && monotonic && chunks.len() == 1,
83    }
84}
85
86/// Rolling min_by/max_by for numeric `by` columns using O(n) deque kernel.
87///
88/// # Panics
89/// Panics if the `by` column's physical dtype is not primitive numeric or if it is a categorical.
90pub fn rolling_numeric_minmax_by(by_col: &Column, slices: &GroupsSlice, is_max_by: bool) -> IdxCa {
91    let dtype = by_col.dtype();
92    let by_series = by_col.as_materialized_series().rechunk();
93    let by_phys = by_series.to_physical_repr();
94    let phys_dtype = by_phys.dtype();
95
96    assert!(
97        phys_dtype.is_primitive_numeric() && !dtype.to_storage().is_categorical(),
98        "rolling_numeric_minmax_by requires a numeric by column, got {dtype}",
99    );
100
101    let arr = with_match_physical_numeric_polars_type!(phys_dtype, |$T| {
102        let ca: &ChunkedArray<$T> = by_phys.as_ref().as_ref().as_ref();
103        let arr = ca.downcast_as_array();
104        let values = arr.values().as_slice();
105        let validity = arr.validity();
106
107        if is_max_by {
108            rolling_argmax_by(values, validity, slices, 1)
109        } else {
110            rolling_argmin_by(values, validity, slices, 1)
111        }
112    });
113
114    IdxCa::with_chunk(PlSmallStr::EMPTY, arr)
115}
116
117// Use an aggregation window that maintains the state
118pub fn _rolling_apply_agg_window_nulls<Agg, T, O, Out>(
119    values: &[T],
120    validity: &Bitmap,
121    offsets: O,
122    params: Option<RollingFnParams>,
123) -> PrimitiveArray<Out>
124where
125    O: Iterator<Item = (IdxSize, IdxSize)> + TrustedLen,
126    Agg: RollingAggWindowNulls<T, Out>,
127    T: IsFloat + NativeType,
128    Out: NativeType,
129{
130    // This iterators length can be trusted
131    // these represent the number of groups in the group_by operation
132    let output_len = offsets.size_hint().0;
133    // start with a dummy index, will be overwritten on first iteration.
134    let mut agg_window = Agg::new(values, validity, 0, 0, params, None);
135
136    let mut validity = MutableBitmap::with_capacity(output_len);
137    validity.extend_constant(output_len, true);
138
139    let out = offsets
140        .enumerate()
141        .map(|(idx, (start, len))| {
142            let end = start + len;
143
144            // SAFETY:
145            // we are in bounds
146            unsafe { agg_window.update(start as usize, end as usize) };
147            match agg_window.get_agg(idx) {
148                Some(val) => val,
149                None => {
150                    // SAFETY: we are in bounds
151                    unsafe { validity.set_unchecked(idx, false) };
152                    Out::default()
153                },
154            }
155        })
156        .collect_trusted::<Vec<_>>();
157
158    PrimitiveArray::new(Out::PRIMITIVE.into(), out.into(), Some(validity.into()))
159}
160
161// Use an aggregation window that maintains the state.
162pub fn _rolling_apply_agg_window_no_nulls<Agg, T, O, Out>(
163    values: &[T],
164    offsets: O,
165    params: Option<RollingFnParams>,
166) -> PrimitiveArray<Out>
167where
168    // items (offset, len) -> so offsets are offset, offset + len
169    Agg: RollingAggWindowNoNulls<T, Out>,
170    O: Iterator<Item = (IdxSize, IdxSize)> + TrustedLen,
171    T: IsFloat + NativeType,
172    Out: NativeType,
173{
174    // start with a dummy index, will be overwritten on first iteration.
175    let mut agg_window = Agg::new(values, 0, 0, params, None);
176
177    offsets
178        .enumerate()
179        .map(|(idx, (start, len))| {
180            let end = start + len;
181
182            // SAFETY: we are in bounds.
183            unsafe { agg_window.update(start as usize, end as usize) };
184            agg_window.get_agg(idx)
185        })
186        .collect::<PrimitiveArray<Out>>()
187}
188
189pub fn _slice_from_offsets<T>(ca: &ChunkedArray<T>, first: IdxSize, len: IdxSize) -> ChunkedArray<T>
190where
191    T: PolarsDataType,
192{
193    ca.slice((first as usize, len as usize))
194}
195
196/// Helper that combines the groups into a parallel iterator over `(first, all): (u32, &Vec<u32>)`.
197pub fn _agg_helper_idx<T, F>(groups: &GroupsIdx, f: F) -> Series
198where
199    F: Fn((IdxSize, &IdxVec)) -> Option<T::Native> + Send + Sync,
200    T: PolarsNumericType,
201{
202    let ca: ChunkedArray<T> = RAYON.install(|| {
203        let first = groups.first();
204        let all = groups.all();
205        collect_primitive_opt_par(groups.len(), |g| f((first[g], &all[g])))
206    });
207    ca.into_series()
208}
209
210/// Same helper as `_agg_helper_idx` but for aggregations that don't return an Option.
211pub fn _agg_helper_idx_no_null<T, F>(groups: &GroupsIdx, f: F) -> Series
212where
213    F: Fn((IdxSize, &IdxVec)) -> T::Native + Send + Sync,
214    T: PolarsNumericType,
215{
216    let ca: NoNull<ChunkedArray<T>> = RAYON.install(|| groups.into_par_iter().map(f).collect());
217    ca.into_inner().into_series()
218}
219
220/// Helper that iterates on the `all: Vec<Vec<u32>` collection,
221/// this doesn't have traverse the `first: Vec<u32>` memory and is therefore faster.
222fn agg_helper_idx_on_all<T, F>(groups: &GroupsIdx, f: F) -> Series
223where
224    F: Fn(&IdxVec) -> Option<T::Native> + Send + Sync,
225    T: PolarsNumericType,
226{
227    agg_helper_idx_on_all_with_scratch::<T, (), _>(groups, |_, _, idx| f(idx))
228}
229
230pub fn _agg_helper_slice<T, F>(groups: &[[IdxSize; 2]], f: F) -> Series
231where
232    F: Fn([IdxSize; 2]) -> Option<T::Native> + Send + Sync,
233    T: PolarsNumericType,
234{
235    agg_helper_slice_with_scratch::<T, (), _>(groups, |_, _, g| f(g))
236}
237
238pub fn _agg_helper_idx_idx<'a, F>(groups: &'a GroupsIdx, f: F) -> Series
239where
240    F: Fn((IdxSize, &'a IdxVec)) -> Option<IdxSize> + Send + Sync,
241{
242    let first = groups.first();
243    let all = groups.all();
244    let ca: IdxCa =
245        RAYON.install(|| collect_primitive_opt_par(groups.len(), |g| f((first[g], &all[g]))));
246    ca.into_series()
247}
248
249pub fn _agg_helper_slice_idx<F>(groups: &[[IdxSize; 2]], f: F) -> Series
250where
251    F: Fn([IdxSize; 2]) -> Option<IdxSize> + Send + Sync,
252{
253    let ca: IdxCa = RAYON.install(|| collect_primitive_opt_par(groups.len(), |g| f(groups[g])));
254    ca.into_series()
255}
256
257pub fn _agg_helper_slice_no_null<T, F>(groups: &[[IdxSize; 2]], f: F) -> Series
258where
259    F: Fn([IdxSize; 2]) -> T::Native + Send + Sync,
260    T: PolarsNumericType,
261{
262    let ca: NoNull<ChunkedArray<T>> = RAYON.install(|| groups.par_iter().copied().map(f).collect());
263    ca.into_inner().into_series()
264}
265
266/// [`agg_helper_idx_on_all`] for an `f` that reuses a scratch value across groups and receives
267/// the group index.
268fn agg_helper_idx_on_all_with_scratch<T, S, F>(groups: &GroupsIdx, f: F) -> Series
269where
270    F: Fn(&mut S, usize, &IdxVec) -> Option<T::Native> + Send + Sync,
271    T: PolarsNumericType,
272    S: Default,
273{
274    let ca: ChunkedArray<T> = RAYON.install(|| {
275        let all = groups.all();
276        collect_primitive_opt_par_with_scratch(groups.len(), S::default, |scratch, g| {
277            f(scratch, g, &all[g])
278        })
279    });
280    ca.into_series()
281}
282
283/// [`_agg_helper_slice`] for an `f` that reuses a scratch value across groups and receives the
284/// group index.
285fn agg_helper_slice_with_scratch<T, S, F>(groups: &[[IdxSize; 2]], f: F) -> Series
286where
287    F: Fn(&mut S, usize, [IdxSize; 2]) -> Option<T::Native> + Send + Sync,
288    T: PolarsNumericType,
289    S: Default,
290{
291    let ca: ChunkedArray<T> = RAYON.install(|| {
292        collect_primitive_opt_par_with_scratch(groups.len(), S::default, |scratch, g| {
293            f(scratch, g, groups[g])
294        })
295    });
296    ca.into_series()
297}
298
299/// The `quantile` of the non-null values at `rows`, gathered into `scratch`.
300///
301/// # Safety
302/// Every row is in bounds of `values` and `validity`.
303unsafe fn quantile_of_rows<N: NumericNative>(
304    scratch: &mut ScratchVec<N>,
305    values: &[N],
306    validity: Option<&Bitmap>,
307    mut rows: impl ExactSizeIterator<Item = usize>,
308    quantile: f64,
309    method: QuantileMethod,
310) -> Option<f64> {
311    let is_valid = |i: usize| validity.is_none_or(|v| unsafe { v.get_bit_unchecked(i) });
312    let value = |i: usize| unsafe { *values.get_unchecked(i) };
313    if rows.len() == 1 {
314        let i = rows.next().unwrap();
315        return is_valid(i).then(|| value(i).to_f64().unwrap());
316    }
317    let vals = scratch.get();
318    // Without a validity the rows keep their exact length, so the copy is a single sized extend.
319    match validity {
320        None => vals.extend(rows.map(value)),
321        Some(_) => vals.extend(rows.filter(|&i| is_valid(i)).map(value)),
322    }
323    quantile_of_nonnull(vals, quantile, method)
324}
325
326/// The `quantile` of `n` values, where `value_at(rank)` reads the value at an ascending rank.
327fn quantile_of_ranked<N: ToPrimitive + Copy + Default>(
328    n: usize,
329    quantile: f64,
330    method: QuantileMethod,
331    value_at: impl Fn(usize) -> N,
332) -> Option<f64> {
333    quantile_with(n, quantile, method, |ranks, out| {
334        for (o, &r) in out.iter_mut().zip(ranks) {
335            *o = value_at(r);
336        }
337    })
338}
339
340/// The quantile to take of each group.
341#[derive(Clone, Copy, Debug)]
342pub enum GroupQuantile<'a> {
343    Scalar(f64),
344    /// One quantile per group; a null quantile gives a null result.
345    PerGroup(&'a Float64Chunked),
346}
347
348unsafe fn agg_quantile_generic<T, K>(
349    ca: &ChunkedArray<T>,
350    groups: &GroupsType,
351    quantile: GroupQuantile<'_>,
352    method: QuantileMethod,
353) -> Series
354where
355    T: PolarsNumericType,
356    K: PolarsNumericType,
357    <K as datatypes::PolarsNumericType>::Native: num_traits::Float + quantile_filter::SealedRolling,
358{
359    let in_range = |q: &f64| (0.0..=1.0).contains(q);
360    let (scalar, per_group) = match quantile {
361        GroupQuantile::Scalar(q) if !in_range(&q) => {
362            return Series::full_null(ca.name().clone(), groups.len(), ca.dtype());
363        },
364        GroupQuantile::Scalar(q) => (Some(q), None),
365        GroupQuantile::PerGroup(qs) => {
366            assert_eq!(qs.len(), groups.len());
367            (None, Some(qs.rechunk()))
368        },
369    };
370    let per_group = per_group.as_deref().map(|qs| qs.downcast_as_array());
371    let quantile_of_group = |g: usize| scalar.or_else(|| per_group?.get(g).filter(in_range));
372    let to_out = |v: f64| -> K::Native { NumCast::from(v).unwrap() };
373    match groups {
374        GroupsType::Idx(groups) => {
375            let ca = ca.rechunk();
376            let arr = ca.downcast_as_array();
377            let values = arr.values().as_slice();
378            let validity = arr.validity();
379            agg_helper_idx_on_all_with_scratch::<K, ScratchVec<_>, _>(groups, |scratch, g, idx| {
380                debug_assert!(idx.iter().all(|&i| (i as usize) < values.len()));
381                let quantile = quantile_of_group(g)?;
382                let rows = idx.iter().map(|&i| i as usize);
383                quantile_of_rows(scratch, values, validity, rows, quantile, method).map(to_out)
384            })
385        },
386        GroupsType::Slice {
387            groups,
388            overlapping,
389            monotonic,
390        } => {
391            if _use_rolling_kernels(groups, *overlapping, *monotonic, ca.chunks()) {
392                // this cast is a no-op for floats
393                let s = ca
394                    .cast_with_options(&K::get_static_dtype(), CastOptions::Overflowing)
395                    .unwrap();
396                let ca: &ChunkedArray<K> = s.as_ref().as_ref();
397                let arr = ca.downcast_iter().next().unwrap();
398                let values = arr.values().as_slice();
399                // The windows only rank the values, so their own probability is unused.
400                let params = Some(RollingFnParams::Quantile(RollingQuantileParams {
401                    prob: 0.5,
402                    method,
403                }));
404                let windows = groups.iter().enumerate();
405                let out: ChunkedArray<K> = match arr.validity() {
406                    None => {
407                        let mut window = QuantileWindow::new(values, 0, 0, params, None);
408                        windows
409                            .map(|(g, [first, len])| {
410                                // SAFETY: the groups are in bounds.
411                                unsafe { window.update(*first as usize, (first + len) as usize) };
412                                let quantile = quantile_of_group(g)?;
413                                quantile_of_ranked(window.n_values(), quantile, method, |r| {
414                                    window.value_at_rank(r)
415                                })
416                                .map(to_out)
417                            })
418                            .collect()
419                    },
420                    Some(validity) => {
421                        let mut window = rolling::nulls::QuantileWindow::new(
422                            values, validity, 0, 0, params, None,
423                        );
424                        windows
425                            .map(|(g, [first, len])| {
426                                // SAFETY: the groups are in bounds.
427                                unsafe { window.update(*first as usize, (first + len) as usize) };
428                                let quantile = quantile_of_group(g)?;
429                                quantile_of_ranked(window.n_values(), quantile, method, |r| {
430                                    window.value_at_rank(r)
431                                })
432                                .map(to_out)
433                            })
434                            .collect()
435                    },
436                };
437                out.into_series()
438            } else if let sorted @ (IsSorted::Ascending | IsSorted::Descending) =
439                ca.is_sorted_flag()
440            {
441                // A contiguous slice of a sorted column is itself sorted, with its nulls at one end,
442                // so each group reads its order statistics directly.
443                let descending = sorted == IsSorted::Descending;
444                agg_helper_slice_with_scratch::<K, (), _>(groups, |_, g, [first, len]| {
445                    debug_assert!(first + len <= ca.len() as IdxSize);
446                    let quantile = quantile_of_group(g)?;
447                    match len {
448                        0 => None,
449                        1 => ca.get(first as usize).map(|v| NumCast::from(v).unwrap()),
450                        _ => {
451                            let group = _slice_from_offsets(ca, first, len);
452                            sorted_quantile(&group, descending, quantile, method).map(to_out)
453                        },
454                    }
455                })
456            } else {
457                let ca = ca.rechunk();
458                let arr = ca.downcast_as_array();
459                let values = arr.values().as_slice();
460                let validity = arr.validity();
461                agg_helper_slice_with_scratch::<K, ScratchVec<_>, _>(
462                    groups,
463                    |scratch, g, [first, len]| {
464                        let (first, len) = (first as usize, len as usize);
465                        debug_assert!(first + len <= values.len());
466                        let quantile = quantile_of_group(g)?;
467                        let rows = first..first + len;
468                        quantile_of_rows(scratch, values, validity, rows, quantile, method)
469                            .map(to_out)
470                    },
471                )
472            }
473        },
474    }
475}
476
477/// # Safety
478///
479/// No bounds checks on `groups`.
480#[cfg(feature = "bitwise")]
481unsafe fn bitwise_agg<T: PolarsNumericType>(
482    ca: &ChunkedArray<T>,
483    groups: &GroupsType,
484    f: fn(&ChunkedArray<T>) -> Option<T::Native>,
485) -> Series
486where
487    ChunkedArray<T>: ChunkTakeUnchecked<[IdxSize]> + ChunkBitwiseReduce<Physical = T::Native>,
488{
489    // Prevent a rechunk for every individual group.
490
491    let s = if groups.len() > 1 {
492        ca.rechunk()
493    } else {
494        Cow::Borrowed(ca)
495    };
496
497    match groups {
498        GroupsType::Idx(groups) => agg_helper_idx_on_all::<T, _>(groups, |idx| {
499            debug_assert!(_idx_in_bounds(idx, s.len()));
500            if idx.is_empty() {
501                None
502            } else {
503                let take = unsafe { s.take_unchecked(idx) };
504                f(&take)
505            }
506        }),
507        GroupsType::Slice { groups, .. } => _agg_helper_slice::<T, _>(groups, |[first, len]| {
508            debug_assert!(len <= s.len() as IdxSize);
509            if len == 0 {
510                None
511            } else {
512                let take = _slice_from_offsets(&s, first, len);
513                f(&take)
514            }
515        }),
516    }
517}
518
519#[cfg(feature = "bitwise")]
520impl<T> ChunkedArray<T>
521where
522    T: PolarsNumericType,
523    ChunkedArray<T>: ChunkTakeUnchecked<[IdxSize]> + ChunkBitwiseReduce<Physical = T::Native>,
524{
525    /// # Safety
526    ///
527    /// No bounds checks on `groups`.
528    pub(crate) unsafe fn agg_and(&self, groups: &GroupsType) -> Series {
529        unsafe { bitwise_agg(self, groups, ChunkBitwiseReduce::and_reduce) }
530    }
531
532    /// # Safety
533    ///
534    /// No bounds checks on `groups`.
535    pub(crate) unsafe fn agg_or(&self, groups: &GroupsType) -> Series {
536        unsafe { bitwise_agg(self, groups, ChunkBitwiseReduce::or_reduce) }
537    }
538
539    /// # Safety
540    ///
541    /// No bounds checks on `groups`.
542    pub(crate) unsafe fn agg_xor(&self, groups: &GroupsType) -> Series {
543        unsafe { bitwise_agg(self, groups, ChunkBitwiseReduce::xor_reduce) }
544    }
545}
546
547/// Group-wise arg min or arg max of `s` in its row-encoding (sort) order, skipping null rows.
548///
549/// # Safety
550///
551/// No bounds checks on `groups`.
552pub(crate) unsafe fn agg_arg_min_max_row_encoded(
553    s: &Series,
554    groups: &GroupsType,
555    is_max: bool,
556) -> Series {
557    let encoded = row_encode::row_encode_ordered_propagate_nulls(s)
558        .cast(&DataType::Binary)
559        .unwrap();
560    let encoded = encoded.binary().unwrap();
561    unsafe {
562        if is_max {
563            encoded.agg_arg_max(groups)
564        } else {
565            encoded.agg_arg_min(groups)
566        }
567    }
568}
569
570/// Group-wise min or max of `s` in its row-encoding (sort) order, skipping null rows.
571///
572/// # Safety
573///
574/// No bounds checks on `groups`.
575pub(crate) unsafe fn agg_min_max_row_encoded(
576    s: &Series,
577    groups: &GroupsType,
578    is_max: bool,
579) -> Series {
580    let idx_in_group = unsafe { agg_arg_min_max_row_encoded(s, groups, is_max) };
581    let idx_in_group = idx_in_group.idx().unwrap();
582    let idx: IdxCa = match groups {
583        GroupsType::Idx(g) => idx_in_group
584            .iter()
585            .zip(g.all())
586            .map(|(i, g)| i.map(|i| unsafe { *g.get_unchecked(i as usize) }))
587            .collect(),
588        GroupsType::Slice { groups, .. } => idx_in_group
589            .iter()
590            .zip(groups)
591            .map(|(i, [first, _])| i.map(|i| first + i))
592            .collect(),
593    };
594    unsafe { s.take_unchecked(&idx) }
595}
596
597impl<T> ChunkedArray<T>
598where
599    T: PolarsNumericType + Sync,
600    T::Native: NativeType + PartialOrd + Num + NumCast + Zero + Bounded + std::iter::Sum<T::Native>,
601    ChunkedArray<T>: ChunkAgg<T::Native>,
602{
603    pub(crate) unsafe fn agg_min(&self, groups: &GroupsType) -> Series {
604        // faster paths
605        if groups.has_sorted_idxs_in_groups() {
606            match self.is_sorted_flag() {
607                IsSorted::Ascending => {
608                    return self.clone().into_series().agg_first_non_null(groups);
609                },
610                IsSorted::Descending => {
611                    return self.clone().into_series().agg_last_non_null(groups);
612                },
613                _ => {},
614            }
615        }
616
617        match groups {
618            GroupsType::Idx(groups) => {
619                let ca = self.rechunk();
620                let arr = ca.downcast_iter().next().unwrap();
621                let no_nulls = arr.null_count() == 0;
622                _agg_helper_idx::<T, _>(groups, |(first, idx)| {
623                    debug_assert!(_idx_in_bounds(idx, arr.len()));
624                    if idx.is_empty() {
625                        None
626                    } else if idx.len() == 1 {
627                        arr.get(first as usize)
628                    } else if no_nulls {
629                        take_agg_no_null_primitive_iter_unchecked(arr, idx2usize(idx))
630                            .reduce(|a, b| a.min_ignore_nan(b))
631                    } else {
632                        take_agg_primitive_iter_unchecked(arr, idx2usize(idx))
633                            .reduce(|a, b| a.min_ignore_nan(b))
634                    }
635                })
636            },
637            GroupsType::Slice {
638                groups: groups_slice,
639                overlapping,
640                monotonic,
641            } => {
642                if _use_rolling_kernels(groups_slice, *overlapping, *monotonic, self.chunks()) {
643                    let arr = self.downcast_iter().next().unwrap();
644                    let values = arr.values().as_slice();
645                    let offset_iter = groups_slice.iter().map(|[first, len]| (*first, *len));
646                    let arr = match arr.validity() {
647                        None => _rolling_apply_agg_window_no_nulls::<MinWindow<_>, _, _, _>(
648                            values,
649                            offset_iter,
650                            None,
651                        ),
652                        Some(validity) => _rolling_apply_agg_window_nulls::<MinWindow<_>, _, _, _>(
653                            values,
654                            validity,
655                            offset_iter,
656                            None,
657                        ),
658                    };
659                    Self::from(arr).into_series()
660                } else {
661                    _agg_helper_slice::<T, _>(groups_slice, |[first, len]| {
662                        debug_assert!(len <= self.len() as IdxSize);
663                        match len {
664                            0 => None,
665                            1 => self.get(first as usize),
666                            _ => {
667                                let arr_group = _slice_from_offsets(self, first, len);
668                                ChunkAgg::min(&arr_group)
669                            },
670                        }
671                    })
672                }
673            },
674        }
675    }
676
677    pub(crate) unsafe fn agg_arg_min(&self, groups: &GroupsType) -> Series
678    where
679        for<'b> &'b [T::Native]: ArgMinMax,
680    {
681        if groups.has_sorted_idxs_in_groups() {
682            match self.is_sorted_flag() {
683                IsSorted::Ascending => {
684                    return self.clone().into_series().agg_arg_first_non_null(groups);
685                },
686                IsSorted::Descending => {
687                    return self.clone().into_series().agg_arg_last_non_null(groups);
688                },
689                _ => {},
690            }
691        }
692
693        match groups {
694            GroupsType::Idx(groups) => {
695                let ca = self.rechunk();
696                let arr = ca.downcast_iter().next().unwrap();
697                let no_nulls = !arr.has_nulls();
698
699                agg_helper_idx_on_all::<IdxType, _>(groups, |idx| {
700                    if idx.is_empty() {
701                        return None;
702                    }
703
704                    if no_nulls {
705                        let first_i = idx[0] as usize;
706                        let mut best_pos: IdxSize = 0;
707                        let mut best_val: T::Native = unsafe { arr.value_unchecked(first_i) };
708
709                        for (pos, &i) in idx.iter().enumerate().skip(1) {
710                            let v = unsafe { arr.value_unchecked(i as usize) };
711                            if v.nan_max_lt(&best_val) {
712                                best_val = v;
713                                best_pos = pos as IdxSize;
714                            }
715                        }
716                        Some(best_pos)
717                    } else {
718                        let (start_pos, mut best_val) =
719                            idx.iter().enumerate().find_map(|(pos, &i)| {
720                                unsafe { arr.get_unchecked(i as usize) }.map(|v| (pos, v))
721                            })?;
722
723                        let mut best_pos: IdxSize = start_pos as IdxSize;
724
725                        for (pos, &i) in idx.iter().enumerate().skip(start_pos + 1) {
726                            if let Some(v) = unsafe { arr.get_unchecked(i as usize) } {
727                                if v.nan_max_lt(&best_val) {
728                                    best_val = v;
729                                    best_pos = pos as IdxSize;
730                                }
731                            }
732                        }
733
734                        Some(best_pos)
735                    }
736                })
737            },
738            GroupsType::Slice {
739                groups: groups_slice,
740                overlapping,
741                monotonic,
742            } => {
743                if _use_rolling_kernels(groups_slice, *overlapping, *monotonic, self.chunks()) {
744                    let arr = self.downcast_as_array();
745                    let values = arr.values().as_slice();
746                    let offset_iter = groups_slice.iter().map(|[first, len]| (*first, *len));
747                    let idx_arr = match arr.validity() {
748                        None => {
749                            _rolling_apply_agg_window_no_nulls::<ArgMinWindow<_>, _, _, IdxSize>(
750                                values,
751                                offset_iter,
752                                None,
753                            )
754                        },
755                        Some(validity) => {
756                            _rolling_apply_agg_window_nulls::<ArgMinWindow<_>, _, _, IdxSize>(
757                                values,
758                                validity,
759                                offset_iter,
760                                None,
761                            )
762                        },
763                    };
764
765                    IdxCa::from(idx_arr).into_series()
766                } else {
767                    _agg_helper_slice::<IdxType, _>(groups_slice, |[first, len]| {
768                        debug_assert!(len <= self.len() as IdxSize);
769                        match len {
770                            0 => None,
771                            1 => unsafe { self.get_unchecked(first as usize) }.map(|_| 0),
772                            _ => {
773                                let group_ca = _slice_from_offsets(self, first, len);
774                                let pos_in_group: Option<usize> = arg_min_numeric(&group_ca);
775                                pos_in_group.map(|p| p as IdxSize)
776                            },
777                        }
778                    })
779                }
780            },
781        }
782    }
783
784    pub(crate) unsafe fn agg_max(&self, groups: &GroupsType) -> Series {
785        // Sorted fast-path. We skip this for floats because the largest value might be NaN, which
786        // max is supposed to skip unless everything is NaN. We would need an
787        // agg_first_non_null_non_nan.
788        if groups.has_sorted_idxs_in_groups() && !T::Native::is_float() {
789            match self.is_sorted_flag() {
790                IsSorted::Ascending => return self.clone().into_series().agg_last_non_null(groups),
791                IsSorted::Descending => {
792                    return self.clone().into_series().agg_first_non_null(groups);
793                },
794                _ => {},
795            }
796        }
797
798        match groups {
799            GroupsType::Idx(groups) => {
800                let ca = self.rechunk();
801                let arr = ca.downcast_iter().next().unwrap();
802                let no_nulls = arr.null_count() == 0;
803                _agg_helper_idx::<T, _>(groups, |(first, idx)| {
804                    debug_assert!(_idx_in_bounds(idx, arr.len()));
805                    if idx.is_empty() {
806                        None
807                    } else if idx.len() == 1 {
808                        arr.get(first as usize)
809                    } else if no_nulls {
810                        take_agg_no_null_primitive_iter_unchecked(arr, idx2usize(idx))
811                            .reduce(|a, b| a.max_ignore_nan(b))
812                    } else {
813                        take_agg_primitive_iter_unchecked(arr, idx2usize(idx))
814                            .reduce(|a, b| a.max_ignore_nan(b))
815                    }
816                })
817            },
818            GroupsType::Slice {
819                groups: groups_slice,
820                overlapping,
821                monotonic,
822            } => {
823                if _use_rolling_kernels(groups_slice, *overlapping, *monotonic, self.chunks()) {
824                    let arr = self.downcast_iter().next().unwrap();
825                    let values = arr.values().as_slice();
826                    let offset_iter = groups_slice.iter().map(|[first, len]| (*first, *len));
827                    let arr = match arr.validity() {
828                        None => _rolling_apply_agg_window_no_nulls::<MaxWindow<_>, _, _, _>(
829                            values,
830                            offset_iter,
831                            None,
832                        ),
833                        Some(validity) => _rolling_apply_agg_window_nulls::<MaxWindow<_>, _, _, _>(
834                            values,
835                            validity,
836                            offset_iter,
837                            None,
838                        ),
839                    };
840                    Self::from(arr).into_series()
841                } else {
842                    _agg_helper_slice::<T, _>(groups_slice, |[first, len]| {
843                        debug_assert!(len <= self.len() as IdxSize);
844                        match len {
845                            0 => None,
846                            1 => self.get(first as usize),
847                            _ => {
848                                let arr_group = _slice_from_offsets(self, first, len);
849                                ChunkAgg::max(&arr_group)
850                            },
851                        }
852                    })
853                }
854            },
855        }
856    }
857
858    pub(crate) unsafe fn agg_arg_max(&self, groups: &GroupsType) -> Series
859    where
860        for<'b> &'b [T::Native]: ArgMinMax,
861    {
862        // Skipped for floats for the same reason as in `agg_max`.
863        if groups.has_sorted_idxs_in_groups() && !T::Native::is_float() {
864            match self.is_sorted_flag() {
865                IsSorted::Ascending => {
866                    return self.clone().into_series().agg_arg_last_non_null(groups);
867                },
868                IsSorted::Descending => {
869                    return self.clone().into_series().agg_arg_first_non_null(groups);
870                },
871                _ => {},
872            }
873        }
874        match groups {
875            GroupsType::Idx(groups) => {
876                let ca = self.rechunk();
877                let arr = ca.downcast_as_array();
878                let no_nulls = arr.null_count() == 0;
879
880                agg_helper_idx_on_all::<IdxType, _>(groups, |idx| {
881                    if idx.is_empty() {
882                        return None;
883                    }
884
885                    if no_nulls {
886                        let first_i = idx[0] as usize;
887                        let mut best_pos: IdxSize = 0;
888                        let mut best_val: T::Native = unsafe { arr.value_unchecked(first_i) };
889
890                        for (pos, &i) in idx.iter().enumerate().skip(1) {
891                            let v = unsafe { arr.value_unchecked(i as usize) };
892
893                            if v.nan_min_gt(&best_val) {
894                                best_val = v;
895                                best_pos = pos as IdxSize;
896                            }
897                        }
898
899                        Some(best_pos)
900                    } else {
901                        let (start_pos, mut best_val) =
902                            idx.iter().enumerate().find_map(|(pos, &i)| {
903                                unsafe { arr.get_unchecked(i as usize) }.map(|v| (pos, v))
904                            })?;
905
906                        let mut best_pos: IdxSize = start_pos as IdxSize;
907
908                        for (pos, &i) in idx.iter().enumerate().skip(start_pos + 1) {
909                            if let Some(v) = unsafe { arr.get_unchecked(i as usize) } {
910                                if v.nan_min_gt(&best_val) {
911                                    best_val = v;
912                                    best_pos = pos as IdxSize;
913                                }
914                            }
915                        }
916
917                        Some(best_pos)
918                    }
919                })
920            },
921
922            GroupsType::Slice {
923                groups: groups_slice,
924                overlapping,
925                monotonic,
926            } => {
927                if _use_rolling_kernels(groups_slice, *overlapping, *monotonic, self.chunks()) {
928                    let arr = self.downcast_iter().next().unwrap();
929                    let values = arr.values().as_slice();
930                    let offset_iter = groups_slice.iter().map(|[first, len]| (*first, *len));
931                    let idx_arr = match arr.validity() {
932                        None => {
933                            _rolling_apply_agg_window_no_nulls::<ArgMaxWindow<_>, _, _, IdxSize>(
934                                values,
935                                offset_iter,
936                                None,
937                            )
938                        },
939                        Some(validity) => {
940                            _rolling_apply_agg_window_nulls::<ArgMaxWindow<_>, _, _, IdxSize>(
941                                values,
942                                validity,
943                                offset_iter,
944                                None,
945                            )
946                        },
947                    };
948                    IdxCa::from(idx_arr).into_series()
949                } else {
950                    _agg_helper_slice::<IdxType, _>(groups_slice, |[first, len]| {
951                        debug_assert!(len <= self.len() as IdxSize);
952                        match len {
953                            0 => None,
954                            1 => unsafe { self.get_unchecked(first as usize) }.map(|_| 0),
955                            _ => {
956                                let group_ca = _slice_from_offsets(self, first, len);
957                                let pos_in_group: Option<usize> = arg_max_numeric(&group_ca);
958                                pos_in_group.map(|p| p as IdxSize)
959                            },
960                        }
961                    })
962                }
963            },
964        }
965    }
966    pub(crate) unsafe fn agg_sum(&self, groups: &GroupsType) -> Series {
967        match groups {
968            GroupsType::Idx(groups) => {
969                let ca = self.rechunk();
970                let arr = ca.downcast_iter().next().unwrap();
971                let no_nulls = arr.null_count() == 0;
972                _agg_helper_idx_no_null::<T, _>(groups, |(first, idx)| {
973                    debug_assert!(_idx_in_bounds(idx, self.len()));
974                    if idx.is_empty() {
975                        T::Native::zero()
976                    } else if idx.len() == 1 {
977                        arr.get(first as usize).unwrap_or(T::Native::zero())
978                    } else if no_nulls {
979                        if T::Native::is_float() {
980                            take_agg_no_null_primitive_iter_unchecked(arr, idx2usize(idx))
981                                .fold(KahanSum::default(), |k, x| k + x)
982                                .sum()
983                        } else {
984                            take_agg_no_null_primitive_iter_unchecked(arr, idx2usize(idx))
985                                .fold(T::Native::zero(), |a, b| a + b)
986                        }
987                    } else if T::Native::is_float() {
988                        take_agg_primitive_iter_unchecked(arr, idx2usize(idx))
989                            .fold(KahanSum::default(), |k, x| k + x)
990                            .sum()
991                    } else {
992                        take_agg_primitive_iter_unchecked(arr, idx2usize(idx))
993                            .fold(T::Native::zero(), |a, b| a + b)
994                    }
995                })
996            },
997            GroupsType::Slice {
998                groups,
999                overlapping,
1000                monotonic,
1001            } => {
1002                if _use_rolling_kernels(groups, *overlapping, *monotonic, self.chunks()) {
1003                    let arr = self.downcast_iter().next().unwrap();
1004                    let values = arr.values().as_slice();
1005                    let offset_iter = groups.iter().map(|[first, len]| (*first, *len));
1006                    let arr = match arr.validity() {
1007                        None => _rolling_apply_agg_window_no_nulls::<
1008                            SumWindow<T::Native, T::Native>,
1009                            _,
1010                            _,
1011                            _,
1012                        >(values, offset_iter, None),
1013                        Some(validity) => {
1014                            _rolling_apply_agg_window_nulls::<
1015                                SumWindow<T::Native, T::Native>,
1016                                _,
1017                                _,
1018                                _,
1019                            >(values, validity, offset_iter, None)
1020                        },
1021                    };
1022                    Self::from(arr).into_series()
1023                } else {
1024                    _agg_helper_slice_no_null::<T, _>(groups, |[first, len]| {
1025                        debug_assert!(len <= self.len() as IdxSize);
1026                        match len {
1027                            0 => T::Native::zero(),
1028                            1 => self.get(first as usize).unwrap_or(T::Native::zero()),
1029                            _ => {
1030                                let arr_group = _slice_from_offsets(self, first, len);
1031                                arr_group.sum().unwrap_or(T::Native::zero())
1032                            },
1033                        }
1034                    })
1035                }
1036            },
1037        }
1038    }
1039}
1040
1041impl<T> SeriesWrap<ChunkedArray<T>>
1042where
1043    T: PolarsFloatType,
1044    ChunkedArray<T>: ChunkVar
1045        + VarAggSeries
1046        + ChunkQuantile<T::Native>
1047        + QuantileAggSeries
1048        + ChunkAgg<T::Native>,
1049    T::Native: Pow<T::Native, Output = T::Native>,
1050{
1051    pub(crate) unsafe fn agg_mean(&self, groups: &GroupsType) -> Series {
1052        match groups {
1053            GroupsType::Idx(groups) => {
1054                let ca = self.rechunk();
1055                let arr = ca.downcast_iter().next().unwrap();
1056                let no_nulls = arr.null_count() == 0;
1057                _agg_helper_idx::<T, _>(groups, |(first, idx)| {
1058                    // this can fail due to a bug in lazy code.
1059                    // here users can create filters in aggregations
1060                    // and thereby creating shorter columns than the original group tuples.
1061                    // the group tuples are modified, but if that's done incorrect there can be out of bounds
1062                    // access
1063                    debug_assert!(_idx_in_bounds(idx, self.len()));
1064                    let out = if idx.is_empty() {
1065                        None
1066                    } else if idx.len() == 1 {
1067                        arr.get(first as usize).map(|sum| sum.to_f64().unwrap())
1068                    } else if no_nulls {
1069                        Some(
1070                            take_agg_no_null_primitive_iter_unchecked(arr, idx2usize(idx))
1071                                .fold(KahanSum::default(), |a, b| {
1072                                    a + b.to_f64().unwrap_unchecked()
1073                                })
1074                                .sum()
1075                                / idx.len() as f64,
1076                        )
1077                    } else {
1078                        take_agg_primitive_iter_unchecked_count_nulls(
1079                            arr,
1080                            idx2usize(idx),
1081                            KahanSum::default(),
1082                            |a, b| a + b.to_f64().unwrap_unchecked(),
1083                            idx.len() as IdxSize,
1084                        )
1085                        .map(|(sum, null_count)| sum.sum() / (idx.len() as f64 - null_count as f64))
1086                    };
1087                    out.map(|flt| NumCast::from(flt).unwrap())
1088                })
1089            },
1090            GroupsType::Slice {
1091                groups,
1092                overlapping,
1093                monotonic,
1094            } => {
1095                if _use_rolling_kernels(groups, *overlapping, *monotonic, self.chunks()) {
1096                    let arr = self.downcast_iter().next().unwrap();
1097                    let values = arr.values().as_slice();
1098                    let offset_iter = groups.iter().map(|[first, len]| (*first, *len));
1099                    let arr = match arr.validity() {
1100                        None => _rolling_apply_agg_window_no_nulls::<MeanWindow<_>, _, _, _>(
1101                            values,
1102                            offset_iter,
1103                            None,
1104                        ),
1105                        Some(validity) => {
1106                            _rolling_apply_agg_window_nulls::<MeanWindow<_>, _, _, _>(
1107                                values,
1108                                validity,
1109                                offset_iter,
1110                                None,
1111                            )
1112                        },
1113                    };
1114                    ChunkedArray::<T>::from(arr).into_series()
1115                } else {
1116                    _agg_helper_slice::<T, _>(groups, |[first, len]| {
1117                        debug_assert!(len <= self.len() as IdxSize);
1118                        match len {
1119                            0 => None,
1120                            1 => self.get(first as usize),
1121                            _ => {
1122                                let arr_group = _slice_from_offsets(self, first, len);
1123                                arr_group.mean().map(|flt| NumCast::from(flt).unwrap())
1124                            },
1125                        }
1126                    })
1127                }
1128            },
1129        }
1130    }
1131
1132    pub(crate) unsafe fn agg_var(&self, groups: &GroupsType, ddof: u8) -> Series
1133    where
1134        <T as datatypes::PolarsNumericType>::Native: num_traits::Float,
1135    {
1136        let ca = &self.0.rechunk();
1137        match groups {
1138            GroupsType::Idx(groups) => {
1139                let ca = ca.rechunk();
1140                let arr = ca.downcast_iter().next().unwrap();
1141                let no_nulls = arr.null_count() == 0;
1142                agg_helper_idx_on_all::<T, _>(groups, |idx| {
1143                    debug_assert!(_idx_in_bounds(idx, ca.len()));
1144                    if idx.is_empty() {
1145                        return None;
1146                    }
1147                    let out = if no_nulls {
1148                        take_var_no_null_primitive_iter_unchecked(arr, idx2usize(idx), ddof)
1149                    } else {
1150                        take_var_nulls_primitive_iter_unchecked(arr, idx2usize(idx), ddof)
1151                    };
1152                    out.map(|flt| NumCast::from(flt).unwrap())
1153                })
1154            },
1155            GroupsType::Slice {
1156                groups,
1157                overlapping,
1158                monotonic,
1159            } => {
1160                if _use_rolling_kernels(groups, *overlapping, *monotonic, self.chunks()) {
1161                    let arr = self.downcast_iter().next().unwrap();
1162                    let values = arr.values().as_slice();
1163                    let offset_iter = groups.iter().map(|[first, len]| (*first, *len));
1164                    let arr = match arr.validity() {
1165                        None => _rolling_apply_agg_window_no_nulls::<
1166                            MomentWindow<_, VarianceMoment>,
1167                            _,
1168                            _,
1169                            _,
1170                        >(
1171                            values,
1172                            offset_iter,
1173                            Some(RollingFnParams::Var(RollingVarParams { ddof })),
1174                        ),
1175                        Some(validity) => _rolling_apply_agg_window_nulls::<
1176                            rolling::nulls::MomentWindow<_, VarianceMoment>,
1177                            _,
1178                            _,
1179                            _,
1180                        >(
1181                            values,
1182                            validity,
1183                            offset_iter,
1184                            Some(RollingFnParams::Var(RollingVarParams { ddof })),
1185                        ),
1186                    };
1187                    ChunkedArray::<T>::from(arr).into_series()
1188                } else {
1189                    _agg_helper_slice::<T, _>(groups, |[first, len]| {
1190                        debug_assert!(len <= self.len() as IdxSize);
1191                        match len {
1192                            0 => None,
1193                            1 => self.get(first as usize).filter(|_| ddof == 0).map(|x| {
1194                                if Float::is_finite(x) {
1195                                    T::Native::zero()
1196                                } else {
1197                                    Float::nan()
1198                                }
1199                            }),
1200                            _ => {
1201                                let arr_group = _slice_from_offsets(self, first, len);
1202                                arr_group.var(ddof).map(|flt| NumCast::from(flt).unwrap())
1203                            },
1204                        }
1205                    })
1206                }
1207            },
1208        }
1209    }
1210    pub(crate) unsafe fn agg_std(&self, groups: &GroupsType, ddof: u8) -> Series
1211    where
1212        <T as datatypes::PolarsNumericType>::Native: num_traits::Float,
1213    {
1214        let ca = &self.0.rechunk();
1215        match groups {
1216            GroupsType::Idx(groups) => {
1217                let arr = ca.downcast_iter().next().unwrap();
1218                let no_nulls = arr.null_count() == 0;
1219                agg_helper_idx_on_all::<T, _>(groups, |idx| {
1220                    debug_assert!(_idx_in_bounds(idx, ca.len()));
1221                    if idx.is_empty() {
1222                        return None;
1223                    }
1224                    let out = if no_nulls {
1225                        take_var_no_null_primitive_iter_unchecked(arr, idx2usize(idx), ddof)
1226                    } else {
1227                        take_var_nulls_primitive_iter_unchecked(arr, idx2usize(idx), ddof)
1228                    };
1229                    out.map(|flt| NumCast::from(flt.sqrt()).unwrap())
1230                })
1231            },
1232            GroupsType::Slice {
1233                groups,
1234                overlapping,
1235                monotonic,
1236            } => {
1237                if _use_rolling_kernels(groups, *overlapping, *monotonic, self.chunks()) {
1238                    let arr = ca.downcast_iter().next().unwrap();
1239                    let values = arr.values().as_slice();
1240                    let offset_iter = groups.iter().map(|[first, len]| (*first, *len));
1241                    let arr = match arr.validity() {
1242                        None => _rolling_apply_agg_window_no_nulls::<
1243                            MomentWindow<_, VarianceMoment>,
1244                            _,
1245                            _,
1246                            _,
1247                        >(
1248                            values,
1249                            offset_iter,
1250                            Some(RollingFnParams::Var(RollingVarParams { ddof })),
1251                        ),
1252                        Some(validity) => _rolling_apply_agg_window_nulls::<
1253                            rolling::nulls::MomentWindow<_, rolling::nulls::VarianceMoment>,
1254                            _,
1255                            _,
1256                            _,
1257                        >(
1258                            values,
1259                            validity,
1260                            offset_iter,
1261                            Some(RollingFnParams::Var(RollingVarParams { ddof })),
1262                        ),
1263                    };
1264
1265                    let mut ca = ChunkedArray::<T>::from(arr);
1266                    ca.apply_mut(|v| v.powf(NumCast::from(0.5).unwrap()));
1267                    ca.into_series()
1268                } else {
1269                    _agg_helper_slice::<T, _>(groups, |[first, len]| {
1270                        debug_assert!(len <= self.len() as IdxSize);
1271                        match len {
1272                            0 => None,
1273                            1 => self.get(first as usize).filter(|_| ddof == 0).map(|x| {
1274                                if Float::is_finite(x) {
1275                                    T::Native::zero()
1276                                } else {
1277                                    Float::nan()
1278                                }
1279                            }),
1280                            _ => {
1281                                let arr_group = _slice_from_offsets(self, first, len);
1282                                arr_group.std(ddof).map(|flt| NumCast::from(flt).unwrap())
1283                            },
1284                        }
1285                    })
1286                }
1287            },
1288        }
1289    }
1290}
1291
1292#[cfg(feature = "dtype-f16")]
1293impl Float16Chunked {
1294    pub(crate) unsafe fn agg_quantile(
1295        &self,
1296        groups: &GroupsType,
1297        quantile: GroupQuantile<'_>,
1298        method: QuantileMethod,
1299    ) -> Series {
1300        agg_quantile_generic::<_, Float16Type>(self, groups, quantile, method)
1301    }
1302    pub(crate) unsafe fn agg_median(&self, groups: &GroupsType) -> Series {
1303        agg_quantile_generic::<_, Float16Type>(
1304            self,
1305            groups,
1306            GroupQuantile::Scalar(0.5),
1307            QuantileMethod::Linear,
1308        )
1309    }
1310}
1311impl Float32Chunked {
1312    pub(crate) unsafe fn agg_quantile(
1313        &self,
1314        groups: &GroupsType,
1315        quantile: GroupQuantile<'_>,
1316        method: QuantileMethod,
1317    ) -> Series {
1318        agg_quantile_generic::<_, Float32Type>(self, groups, quantile, method)
1319    }
1320    pub(crate) unsafe fn agg_median(&self, groups: &GroupsType) -> Series {
1321        agg_quantile_generic::<_, Float32Type>(
1322            self,
1323            groups,
1324            GroupQuantile::Scalar(0.5),
1325            QuantileMethod::Linear,
1326        )
1327    }
1328}
1329impl Float64Chunked {
1330    pub(crate) unsafe fn agg_quantile(
1331        &self,
1332        groups: &GroupsType,
1333        quantile: GroupQuantile<'_>,
1334        method: QuantileMethod,
1335    ) -> Series {
1336        agg_quantile_generic::<_, Float64Type>(self, groups, quantile, method)
1337    }
1338    pub(crate) unsafe fn agg_median(&self, groups: &GroupsType) -> Series {
1339        agg_quantile_generic::<_, Float64Type>(
1340            self,
1341            groups,
1342            GroupQuantile::Scalar(0.5),
1343            QuantileMethod::Linear,
1344        )
1345    }
1346}
1347
1348impl<T> ChunkedArray<T>
1349where
1350    T: PolarsIntegerType,
1351    ChunkedArray<T>: ChunkAgg<T::Native> + ChunkVar,
1352    T::Native: NumericNative + Ord,
1353{
1354    pub(crate) unsafe fn agg_mean(&self, groups: &GroupsType) -> Series {
1355        match groups {
1356            GroupsType::Idx(groups) => {
1357                let ca = self.rechunk();
1358                let arr = ca.downcast_get(0).unwrap();
1359                _agg_helper_idx::<Float64Type, _>(groups, |(first, idx)| {
1360                    // this can fail due to a bug in lazy code.
1361                    // here users can create filters in aggregations
1362                    // and thereby creating shorter columns than the original group tuples.
1363                    // the group tuples are modified, but if that's done incorrect there can be out of bounds
1364                    // access
1365                    debug_assert!(_idx_in_bounds(idx, self.len()));
1366                    if idx.is_empty() {
1367                        None
1368                    } else if idx.len() == 1 {
1369                        self.get(first as usize).map(|sum| sum.to_f64().unwrap())
1370                    } else {
1371                        match (self.has_nulls(), self.chunks.len()) {
1372                            (false, 1) => Some(
1373                                take_agg_no_null_primitive_iter_unchecked(arr, idx2usize(idx))
1374                                    .fold(KahanSum::default(), |a, b| a + b.to_f64().unwrap())
1375                                    .sum()
1376                                    / idx.len() as f64,
1377                            ),
1378                            (_, 1) => {
1379                                take_agg_primitive_iter_unchecked_count_nulls(
1380                                    arr,
1381                                    idx2usize(idx),
1382                                    KahanSum::default(),
1383                                    |a, b| a + b.to_f64().unwrap(),
1384                                    idx.len() as IdxSize,
1385                                )
1386                            }
1387                            .map(|(sum, null_count)| {
1388                                sum.sum() / (idx.len() as f64 - null_count as f64)
1389                            }),
1390                            _ => {
1391                                let take = { self.take_unchecked(idx) };
1392                                take.mean()
1393                            },
1394                        }
1395                    }
1396                })
1397            },
1398            GroupsType::Slice {
1399                groups: groups_slice,
1400                overlapping,
1401                monotonic,
1402            } => {
1403                if _use_rolling_kernels(groups_slice, *overlapping, *monotonic, self.chunks()) {
1404                    let ca = self
1405                        .cast_with_options(&DataType::Float64, CastOptions::Overflowing)
1406                        .unwrap();
1407                    ca.agg_mean(groups)
1408                } else {
1409                    _agg_helper_slice::<Float64Type, _>(groups_slice, |[first, len]| {
1410                        debug_assert!(first + len <= self.len() as IdxSize);
1411                        match len {
1412                            0 => None,
1413                            1 => self.get(first as usize).map(|v| NumCast::from(v).unwrap()),
1414                            _ => {
1415                                let arr_group = _slice_from_offsets(self, first, len);
1416                                arr_group.mean()
1417                            },
1418                        }
1419                    })
1420                }
1421            },
1422        }
1423    }
1424
1425    pub(crate) unsafe fn agg_var(&self, groups: &GroupsType, ddof: u8) -> Series {
1426        match groups {
1427            GroupsType::Idx(groups) => {
1428                let ca_self = self.rechunk();
1429                let arr = ca_self.downcast_iter().next().unwrap();
1430                let no_nulls = arr.null_count() == 0;
1431                agg_helper_idx_on_all::<Float64Type, _>(groups, |idx| {
1432                    debug_assert!(_idx_in_bounds(idx, arr.len()));
1433                    if idx.is_empty() {
1434                        return None;
1435                    }
1436                    if no_nulls {
1437                        take_var_no_null_primitive_iter_unchecked(arr, idx2usize(idx), ddof)
1438                    } else {
1439                        take_var_nulls_primitive_iter_unchecked(arr, idx2usize(idx), ddof)
1440                    }
1441                })
1442            },
1443            GroupsType::Slice {
1444                groups: groups_slice,
1445                overlapping,
1446                monotonic,
1447            } => {
1448                if _use_rolling_kernels(groups_slice, *overlapping, *monotonic, self.chunks()) {
1449                    let ca = self
1450                        .cast_with_options(&DataType::Float64, CastOptions::Overflowing)
1451                        .unwrap();
1452                    ca.agg_var(groups, ddof)
1453                } else {
1454                    _agg_helper_slice::<Float64Type, _>(groups_slice, |[first, len]| {
1455                        debug_assert!(first + len <= self.len() as IdxSize);
1456                        match len {
1457                            0 => None,
1458                            1 => self.get(first as usize).filter(|_| ddof == 0).map(|_| 0.0),
1459                            _ => {
1460                                let arr_group = _slice_from_offsets(self, first, len);
1461                                arr_group.var(ddof)
1462                            },
1463                        }
1464                    })
1465                }
1466            },
1467        }
1468    }
1469    pub(crate) unsafe fn agg_std(&self, groups: &GroupsType, ddof: u8) -> Series {
1470        match groups {
1471            GroupsType::Idx(groups) => {
1472                let ca_self = self.rechunk();
1473                let arr = ca_self.downcast_iter().next().unwrap();
1474                let no_nulls = arr.null_count() == 0;
1475                agg_helper_idx_on_all::<Float64Type, _>(groups, |idx| {
1476                    debug_assert!(_idx_in_bounds(idx, self.len()));
1477                    if idx.is_empty() {
1478                        return None;
1479                    }
1480                    let out = if no_nulls {
1481                        take_var_no_null_primitive_iter_unchecked(arr, idx2usize(idx), ddof)
1482                    } else {
1483                        take_var_nulls_primitive_iter_unchecked(arr, idx2usize(idx), ddof)
1484                    };
1485                    out.map(|v| v.sqrt())
1486                })
1487            },
1488            GroupsType::Slice {
1489                groups: groups_slice,
1490                overlapping,
1491                monotonic,
1492            } => {
1493                if _use_rolling_kernels(groups_slice, *overlapping, *monotonic, self.chunks()) {
1494                    let ca = self
1495                        .cast_with_options(&DataType::Float64, CastOptions::Overflowing)
1496                        .unwrap();
1497                    ca.agg_std(groups, ddof)
1498                } else {
1499                    _agg_helper_slice::<Float64Type, _>(groups_slice, |[first, len]| {
1500                        debug_assert!(first + len <= self.len() as IdxSize);
1501                        match len {
1502                            0 => None,
1503                            1 => self.get(first as usize).filter(|_| ddof == 0).map(|_| 0.0),
1504                            _ => {
1505                                let arr_group = _slice_from_offsets(self, first, len);
1506                                arr_group.std(ddof)
1507                            },
1508                        }
1509                    })
1510                }
1511            },
1512        }
1513    }
1514
1515    pub(crate) unsafe fn agg_quantile(
1516        &self,
1517        groups: &GroupsType,
1518        quantile: GroupQuantile<'_>,
1519        method: QuantileMethod,
1520    ) -> Series {
1521        agg_quantile_generic::<_, Float64Type>(self, groups, quantile, method)
1522    }
1523    pub(crate) unsafe fn agg_median(&self, groups: &GroupsType) -> Series {
1524        agg_quantile_generic::<_, Float64Type>(
1525            self,
1526            groups,
1527            GroupQuantile::Scalar(0.5),
1528            QuantileMethod::Linear,
1529        )
1530    }
1531}