Skip to main content

polars_core/frame/group_by/
mod.rs

1use std::fmt::{Debug, Display, Formatter};
2use std::hash::Hash;
3
4use num_traits::NumCast;
5use polars_compute::rolling::QuantileMethod;
6use polars_utils::broadcast::broadcast_len;
7use polars_utils::format_pl_smallstr;
8use polars_utils::hashing::DirtyHash;
9use rayon::prelude::*;
10
11use self::hashing::*;
12use crate::prelude::*;
13use crate::runtime::RAYON;
14use crate::utils::{_set_partition_size, accumulate_dataframes_vertical};
15
16pub mod aggregations;
17pub(crate) mod hashing;
18mod into_groups;
19mod position;
20
21pub use into_groups::*;
22pub use position::*;
23
24use crate::chunked_array::ops::row_encode::{
25    encode_rows_unordered, encode_rows_vertical_par_unordered,
26};
27
28impl DataFrame {
29    pub fn group_by_with_series(
30        &self,
31        mut by: Vec<Column>,
32        multithreaded: bool,
33        sorted: bool,
34    ) -> PolarsResult<GroupBy<'_>> {
35        polars_ensure!(
36            !by.is_empty(),
37            ComputeError: "at least one key is required in a group_by operation"
38        );
39
40        // Ensure all 'by' columns have the same common_height
41        // The condition self.width > 0 ensures we can still call this on a
42        // dummy dataframe where we provide the keys
43        let common_height = if self.width() > 0 {
44            self.height()
45        } else {
46            broadcast_len(by.iter()).context("group_by key")?
47        };
48        for by_key in by.iter_mut() {
49            by_key
50                .broadcast_in_place_to(common_height)
51                .context("group_by keys should have the same length as the DataFrame")?;
52        }
53
54        let groups = if by.len() == 1 {
55            let column = &by[0];
56            column
57                .as_materialized_series()
58                .group_tuples(multithreaded, sorted)
59        } else if by.iter().any(|s| s.dtype().is_object()) {
60            #[cfg(feature = "object")]
61            {
62                let mut df = DataFrame::new(self.height(), by.clone()).unwrap();
63                let n = df.height();
64                let rows = df.to_av_rows();
65                let iter = (0..n).map(|i| rows.get(i));
66                Ok(group_by(iter, sorted))
67            }
68            #[cfg(not(feature = "object"))]
69            {
70                unreachable!()
71            }
72        } else {
73            // Skip null dtype.
74            let by = by
75                .iter()
76                .filter(|s| !s.dtype().is_null())
77                .cloned()
78                .collect::<Vec<_>>();
79            if by.is_empty() {
80                let groups = if self.height() == 0 {
81                    vec![]
82                } else {
83                    vec![[0, self.height() as IdxSize]]
84                };
85
86                Ok(GroupsType::new_slice(groups, false, true))
87            } else {
88                let rows = if multithreaded {
89                    encode_rows_vertical_par_unordered(&by)
90                } else {
91                    encode_rows_unordered(&by)
92                }?
93                .into_series();
94                rows.group_tuples(multithreaded, sorted)
95            }
96        };
97        Ok(GroupBy::new(self, by, groups?.into_sliceable(), None))
98    }
99
100    /// Group DataFrame using a Series column.
101    ///
102    /// # Example
103    ///
104    /// ```
105    /// use polars_core::prelude::*;
106    /// fn group_by_example(df: &DataFrame) -> PolarsResult<DataFrame> {
107    ///     df.group_by(["column_name"])?
108    ///     .select(["agg_column_name"])
109    ///     .groups()
110    /// }
111    /// ```
112    pub fn group_by<I, S>(&self, by: I) -> PolarsResult<GroupBy<'_>>
113    where
114        I: IntoIterator<Item = S>,
115        S: AsRef<str>,
116    {
117        let selected_keys = self.select_to_vec(by)?;
118        self.group_by_with_series(selected_keys, true, false)
119    }
120
121    /// Group DataFrame using a Series column.
122    /// The groups are ordered by their smallest row index.
123    pub fn group_by_stable<I, S>(&self, by: I) -> PolarsResult<GroupBy<'_>>
124    where
125        I: IntoIterator<Item = S>,
126        S: AsRef<str>,
127    {
128        let selected_keys = self.select_to_vec(by)?;
129        self.group_by_with_series(selected_keys, true, true)
130    }
131}
132
133/// Returned by a group_by operation on a DataFrame. This struct supports
134/// several aggregations.
135///
136/// Until described otherwise, the examples in this struct are performed on the following DataFrame:
137///
138/// ```ignore
139/// use polars_core::prelude::*;
140///
141/// let dates = &[
142/// "2020-08-21",
143/// "2020-08-21",
144/// "2020-08-22",
145/// "2020-08-23",
146/// "2020-08-22",
147/// ];
148/// // date format
149/// let fmt = "%Y-%m-%d";
150/// // create date series
151/// let s0 = DateChunked::parse_from_str_slice("date", dates, fmt)
152///         .into_series();
153/// // create temperature series
154/// let s1 = Series::new("temp".into(), [20, 10, 7, 9, 1]);
155/// // create rain series
156/// let s2 = Series::new("rain".into(), [0.2, 0.1, 0.3, 0.1, 0.01]);
157/// // create a new DataFrame
158/// let df = DataFrame::new_infer_height(vec![s0, s1, s2]).unwrap();
159/// println!("{:?}", df);
160/// ```
161///
162/// Outputs:
163///
164/// ```text
165/// +------------+------+------+
166/// | date       | temp | rain |
167/// | ---        | ---  | ---  |
168/// | Date       | i32  | f64  |
169/// +============+======+======+
170/// | 2020-08-21 | 20   | 0.2  |
171/// +------------+------+------+
172/// | 2020-08-21 | 10   | 0.1  |
173/// +------------+------+------+
174/// | 2020-08-22 | 7    | 0.3  |
175/// +------------+------+------+
176/// | 2020-08-23 | 9    | 0.1  |
177/// +------------+------+------+
178/// | 2020-08-22 | 1    | 0.01 |
179/// +------------+------+------+
180/// ```
181///
182#[derive(Debug, Clone)]
183pub struct GroupBy<'a> {
184    pub df: &'a DataFrame,
185    pub(crate) selected_keys: Vec<Column>,
186    // [first idx, [other idx]]
187    groups: GroupPositions,
188    // columns selected for aggregation
189    pub(crate) selected_agg: Option<Vec<PlSmallStr>>,
190}
191
192impl<'a> GroupBy<'a> {
193    pub fn new(
194        df: &'a DataFrame,
195        by: Vec<Column>,
196        groups: GroupPositions,
197        selected_agg: Option<Vec<PlSmallStr>>,
198    ) -> Self {
199        GroupBy {
200            df,
201            selected_keys: by,
202            groups,
203            selected_agg,
204        }
205    }
206
207    /// Select the column(s) that should be aggregated.
208    /// You can select a single column or a slice of columns.
209    ///
210    /// Note that making a selection with this method is not required. If you
211    /// skip it all columns (except for the keys) will be selected for aggregation.
212    #[must_use]
213    pub fn select<I: IntoIterator<Item = S>, S: Into<PlSmallStr>>(mut self, selection: I) -> Self {
214        self.selected_agg = Some(selection.into_iter().map(|s| s.into()).collect());
215        self
216    }
217
218    /// Get the internal representation of the GroupBy operation.
219    /// The Vec returned contains:
220    ///     (first_idx, [`Vec<indexes>`])
221    ///     Where second value in the tuple is a vector with all matching indexes.
222    pub fn get_groups(&self) -> &GroupPositions {
223        &self.groups
224    }
225
226    pub fn into_groups(self) -> GroupPositions {
227        self.groups
228    }
229
230    pub fn keys_sliced(&self, slice: Option<(i64, usize)>) -> Vec<Column> {
231        #[allow(unused_assignments)]
232        // needed to keep the lifetimes valid for this scope
233        let mut groups_owned = None;
234
235        let groups = if let Some((offset, len)) = slice {
236            groups_owned = Some(self.groups.slice(offset, len));
237            groups_owned.as_deref().unwrap()
238        } else {
239            &self.groups
240        };
241        RAYON.install(|| {
242            self.selected_keys
243                .par_iter()
244                .map(Column::as_materialized_series)
245                .map(|s| {
246                    match groups {
247                        GroupsType::Idx(groups) => {
248                            // SAFETY: groups are always in bounds.
249                            let mut out = unsafe { s.take_slice_unchecked(groups.first()) };
250                            if groups.sorted_by_first_idx {
251                                out.set_sorted_flag(s.is_sorted_flag());
252                            };
253                            out
254                        },
255                        GroupsType::Slice {
256                            groups,
257                            overlapping,
258                            monotonic: _,
259                        } => {
260                            if *overlapping && !groups.is_empty() {
261                                // Groups can be sliced.
262                                let offset = groups[0][0];
263                                let [upper_offset, upper_len] = groups[groups.len() - 1];
264                                return s.slice(
265                                    offset as i64,
266                                    ((upper_offset + upper_len) - offset) as usize,
267                                );
268                            }
269
270                            let indices = groups
271                                .iter()
272                                .map(|&[first, _len]| first)
273                                .collect_ca(PlSmallStr::EMPTY);
274                            // SAFETY: groups are always in bounds.
275                            let mut out = unsafe { s.take_unchecked(&indices) };
276                            // Sliced groups are always in order of discovery.
277                            out.set_sorted_flag(s.is_sorted_flag());
278                            out
279                        },
280                    }
281                })
282                .map(Column::from)
283                .collect()
284        })
285    }
286
287    pub fn keys(&self) -> Vec<Column> {
288        self.keys_sliced(None)
289    }
290
291    fn prepare_agg(&self) -> PolarsResult<(Vec<Column>, Vec<Column>)> {
292        let keys = self.keys();
293
294        let agg_col = match &self.selected_agg {
295            Some(selection) => self.df.select_to_vec(selection),
296            None => {
297                let by: Vec<_> = self.selected_keys.iter().map(|s| s.name()).collect();
298                let selection = self
299                    .df
300                    .columns()
301                    .iter()
302                    .map(|s| s.name())
303                    .filter(|a| !by.contains(a))
304                    .cloned()
305                    .collect::<Vec<_>>();
306
307                self.df.select_to_vec(selection.as_slice())
308            },
309        }?;
310
311        Ok((keys, agg_col))
312    }
313
314    /// Aggregate grouped series and compute the number of values per group.
315    ///
316    /// # Example
317    ///
318    /// ```rust
319    /// # use polars_core::prelude::*;
320    /// fn example(df: DataFrame) -> PolarsResult<DataFrame> {
321    ///     df.group_by(["date"])?.select(["temp"]).count()
322    /// }
323    /// ```
324    /// Returns:
325    ///
326    /// ```text
327    /// +------------+------------+
328    /// | date       | temp_count |
329    /// | ---        | ---        |
330    /// | Date       | u32        |
331    /// +============+============+
332    /// | 2020-08-23 | 1          |
333    /// +------------+------------+
334    /// | 2020-08-22 | 2          |
335    /// +------------+------------+
336    /// | 2020-08-21 | 2          |
337    /// +------------+------------+
338    /// ```
339    pub fn count(&self) -> PolarsResult<DataFrame> {
340        let (mut cols, agg_cols) = self.prepare_agg()?;
341
342        for agg_col in agg_cols {
343            let new_name = fmt_group_by_column(
344                agg_col.name().as_str(),
345                GroupByMethod::Count {
346                    include_nulls: true,
347                },
348            );
349            let mut ca = self.groups.group_count();
350            ca.rename(new_name);
351            cols.push(ca.into_column());
352        }
353        DataFrame::new_infer_height(cols)
354    }
355
356    /// Get the group_by group indexes.
357    ///
358    /// # Example
359    ///
360    /// ```rust
361    /// # use polars_core::prelude::*;
362    /// fn example(df: DataFrame) -> PolarsResult<DataFrame> {
363    ///     df.group_by(["date"])?.groups()
364    /// }
365    /// ```
366    /// Returns:
367    ///
368    /// ```text
369    /// +--------------+------------+
370    /// | date         | groups     |
371    /// | ---          | ---        |
372    /// | Date(days)   | list [u32] |
373    /// +==============+============+
374    /// | 2020-08-23   | "[3]"      |
375    /// +--------------+------------+
376    /// | 2020-08-22   | "[2, 4]"   |
377    /// +--------------+------------+
378    /// | 2020-08-21   | "[0, 1]"   |
379    /// +--------------+------------+
380    /// ```
381    pub fn groups(&self) -> PolarsResult<DataFrame> {
382        let mut cols = self.keys();
383        let mut column = self.groups.as_list_chunked();
384        let new_name = fmt_group_by_column("", GroupByMethod::Groups);
385        column.rename(new_name);
386        cols.push(column.into_column());
387        DataFrame::new_infer_height(cols)
388    }
389
390    fn prepare_apply(&self) -> PolarsResult<DataFrame> {
391        if let Some(agg) = &self.selected_agg {
392            if agg.is_empty() {
393                Ok(self.df.clone())
394            } else {
395                let mut new_cols = Vec::with_capacity(self.selected_keys.len() + agg.len());
396                new_cols.extend_from_slice(&self.selected_keys);
397                let cols = self.df.select_to_vec(agg.as_slice())?;
398                new_cols.extend(cols);
399                Ok(unsafe { DataFrame::new_unchecked(self.df.height(), new_cols) })
400            }
401        } else {
402            Ok(self.df.clone())
403        }
404    }
405
406    /// Apply a closure over the groups as a new [`DataFrame`].
407    pub fn apply<F>(&self, f: F) -> PolarsResult<DataFrame>
408    where
409        F: FnMut(DataFrame) -> PolarsResult<DataFrame> + Send + Sync,
410    {
411        self.apply_sliced(None, f, None)
412    }
413
414    pub fn apply_sliced<F>(
415        &self,
416        slice: Option<(i64, usize)>,
417        mut f: F,
418        schema: Option<&SchemaRef>,
419    ) -> PolarsResult<DataFrame>
420    where
421        F: FnMut(DataFrame) -> PolarsResult<DataFrame> + Send + Sync,
422    {
423        if self.df.height() == 0 {
424            // return empty dataframe with correct schema
425            if let Some(schema) = schema {
426                return Ok(DataFrame::empty_with_arc_schema(schema.clone()));
427            }
428
429            polars_bail!(ComputeError: "cannot group_by + apply on empty 'DataFrame'");
430        }
431
432        let df = self.prepare_apply()?;
433        let max_height = if let Some((offset, len)) = slice {
434            offset.try_into().unwrap_or(usize::MAX).saturating_add(len)
435        } else {
436            usize::MAX
437        };
438        let mut height = 0;
439        let mut dfs = Vec::with_capacity(self.get_groups().len());
440        for g in self.get_groups().iter() {
441            // SAFETY: groups are in bounds.
442            let sub_df = unsafe { take_df(&df, g) };
443            let df = f(sub_df)?;
444            height += df.height();
445            dfs.push(df);
446
447            // Even if max_height is zero we need at least one df, so check
448            // after first push.
449            if height >= max_height {
450                break;
451            }
452        }
453
454        let mut df = accumulate_dataframes_vertical(dfs)?;
455        if let Some((offset, len)) = slice {
456            df = df.slice(offset, len);
457        }
458        Ok(df)
459    }
460
461    pub fn sliced(mut self, slice: Option<(i64, usize)>) -> Self {
462        match slice {
463            None => self,
464            Some((offset, length)) => {
465                self.groups = self.groups.slice(offset, length);
466                self.selected_keys = self.keys_sliced(slice);
467                self
468            },
469        }
470    }
471}
472
473unsafe fn take_df(df: &DataFrame, g: GroupsIndicator) -> DataFrame {
474    match g {
475        GroupsIndicator::Idx(idx) => df.take_slice_unchecked(idx.1),
476        GroupsIndicator::Slice([first, len]) => df.slice(first as i64, len as usize),
477    }
478}
479
480#[derive(Copy, Clone, Debug)]
481pub enum GroupByMethod {
482    Min,
483    NanMin,
484    Max,
485    NanMax,
486    Median,
487    Mean,
488    First,
489    FirstNonNull,
490    Last,
491    LastNonNull,
492    Item { allow_empty: bool },
493    Sum,
494    Groups,
495    NUnique,
496    Quantile(f64, QuantileMethod),
497    Count { include_nulls: bool },
498    Implode { maintain_order: bool },
499    Std(u8),
500    Var(u8),
501    ArgMin,
502    ArgMax,
503}
504
505impl Display for GroupByMethod {
506    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
507        use GroupByMethod::*;
508        let s = match self {
509            Min => "min",
510            NanMin => "nan_min",
511            Max => "max",
512            NanMax => "nan_max",
513            Median => "median",
514            Mean => "mean",
515            First => "first",
516            FirstNonNull => "first_non_null",
517            Last => "last",
518            LastNonNull => "last_non_null",
519            Item { .. } => "item",
520            Sum => "sum",
521            Groups => "groups",
522            NUnique => "n_unique",
523            Quantile(_, _) => "quantile",
524            Count { .. } => "count",
525            Implode { .. } => "implode",
526            Std(_) => "std",
527            Var(_) => "var",
528            ArgMin => "arg_min",
529            ArgMax => "arg_max",
530        };
531        write!(f, "{s}")
532    }
533}
534
535// Formatting functions used in eager and lazy code for renaming grouped columns
536pub fn fmt_group_by_column(name: &str, method: GroupByMethod) -> PlSmallStr {
537    use GroupByMethod::*;
538    match method {
539        Min => format_pl_smallstr!("{name}_min"),
540        Max => format_pl_smallstr!("{name}_max"),
541        NanMin => format_pl_smallstr!("{name}_nan_min"),
542        NanMax => format_pl_smallstr!("{name}_nan_max"),
543        Median => format_pl_smallstr!("{name}_median"),
544        Mean => format_pl_smallstr!("{name}_mean"),
545        First => format_pl_smallstr!("{name}_first"),
546        FirstNonNull => format_pl_smallstr!("{name}_first_non_null"),
547        Last => format_pl_smallstr!("{name}_last"),
548        LastNonNull => format_pl_smallstr!("{name}_last_non_null"),
549        Item { .. } => format_pl_smallstr!("{name}_item"),
550        Sum => format_pl_smallstr!("{name}_sum"),
551        Groups => PlSmallStr::from_static("groups"),
552        NUnique => format_pl_smallstr!("{name}_n_unique"),
553        Count { .. } => format_pl_smallstr!("{name}_count"),
554        Implode { .. } => format_pl_smallstr!("{name}_agg_list"),
555        Quantile(quantile, _interpol) => format_pl_smallstr!("{name}_quantile_{quantile:.2}"),
556        Std(_) => format_pl_smallstr!("{name}_agg_std"),
557        Var(_) => format_pl_smallstr!("{name}_agg_var"),
558        ArgMin => format_pl_smallstr!("{name}_arg_min"),
559        ArgMax => format_pl_smallstr!("{name}_arg_max"),
560    }
561}