1use std::fmt::{Debug, Display, Formatter};
2use std::hash::Hash;
3
4use num_traits::NumCast;
5use polars_compute::rolling::QuantileMethod;
6use polars_utils::format_pl_smallstr;
7use polars_utils::hashing::DirtyHash;
8use rayon::prelude::*;
9
10use self::hashing::*;
11use crate::POOL;
12use crate::prelude::*;
13use crate::utils::{_set_partition_size, accumulate_dataframes_vertical};
14
15pub mod aggregations;
16pub mod expr;
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 let common_height = if self.width() > 0 {
44 self.height()
45 } else {
46 by.iter().map(|s| s.len()).max().expect("at least 1 key")
47 };
48 for by_key in by.iter_mut() {
49 if by_key.len() != common_height {
50 polars_ensure!(
51 by_key.len() == 1,
52 ShapeMismatch: "series used as keys should have the same length as the DataFrame"
53 );
54 *by_key = by_key.new_from_index(0, common_height)
55 }
56 }
57
58 let groups = if by.len() == 1 {
59 let column = &by[0];
60 column
61 .as_materialized_series()
62 .group_tuples(multithreaded, sorted)
63 } else if by.iter().any(|s| s.dtype().is_object()) {
64 #[cfg(feature = "object")]
65 {
66 let mut df = DataFrame::new(by.clone()).unwrap();
67 let n = df.height();
68 let rows = df.to_av_rows();
69 let iter = (0..n).map(|i| rows.get(i));
70 Ok(group_by(iter, sorted))
71 }
72 #[cfg(not(feature = "object"))]
73 {
74 unreachable!()
75 }
76 } else {
77 let by = by
79 .iter()
80 .filter(|s| !s.dtype().is_null())
81 .cloned()
82 .collect::<Vec<_>>();
83 if by.is_empty() {
84 let groups = if self.is_empty() {
85 vec![]
86 } else {
87 vec![[0, self.height() as IdxSize]]
88 };
89 Ok(GroupsType::Slice {
90 groups,
91 rolling: false,
92 })
93 } else {
94 let rows = if multithreaded {
95 encode_rows_vertical_par_unordered(&by)
96 } else {
97 encode_rows_unordered(&by)
98 }?
99 .into_series();
100 rows.group_tuples(multithreaded, sorted)
101 }
102 };
103 Ok(GroupBy::new(self, by, groups?.into_sliceable(), None))
104 }
105
106 pub fn group_by<I, S>(&self, by: I) -> PolarsResult<GroupBy<'_>>
119 where
120 I: IntoIterator<Item = S>,
121 S: Into<PlSmallStr>,
122 {
123 let selected_keys = self.select_columns(by)?;
124 self.group_by_with_series(selected_keys, true, false)
125 }
126
127 pub fn group_by_stable<I, S>(&self, by: I) -> PolarsResult<GroupBy<'_>>
130 where
131 I: IntoIterator<Item = S>,
132 S: Into<PlSmallStr>,
133 {
134 let selected_keys = self.select_columns(by)?;
135 self.group_by_with_series(selected_keys, true, true)
136 }
137}
138
139#[derive(Debug, Clone)]
189pub struct GroupBy<'a> {
190 pub df: &'a DataFrame,
191 pub(crate) selected_keys: Vec<Column>,
192 groups: GroupPositions,
194 pub(crate) selected_agg: Option<Vec<PlSmallStr>>,
196}
197
198impl<'a> GroupBy<'a> {
199 pub fn new(
200 df: &'a DataFrame,
201 by: Vec<Column>,
202 groups: GroupPositions,
203 selected_agg: Option<Vec<PlSmallStr>>,
204 ) -> Self {
205 GroupBy {
206 df,
207 selected_keys: by,
208 groups,
209 selected_agg,
210 }
211 }
212
213 #[must_use]
219 pub fn select<I: IntoIterator<Item = S>, S: Into<PlSmallStr>>(mut self, selection: I) -> Self {
220 self.selected_agg = Some(selection.into_iter().map(|s| s.into()).collect());
221 self
222 }
223
224 pub fn get_groups(&self) -> &GroupPositions {
229 &self.groups
230 }
231
232 pub unsafe fn get_groups_mut(&mut self) -> &mut GroupPositions {
241 &mut self.groups
242 }
243
244 pub fn take_groups(self) -> GroupPositions {
245 self.groups
246 }
247
248 pub fn take_groups_mut(&mut self) -> GroupPositions {
249 std::mem::take(&mut self.groups)
250 }
251
252 pub fn keys_sliced(&self, slice: Option<(i64, usize)>) -> Vec<Column> {
253 #[allow(unused_assignments)]
254 let mut groups_owned = None;
256
257 let groups = if let Some((offset, len)) = slice {
258 groups_owned = Some(self.groups.slice(offset, len));
259 groups_owned.as_deref().unwrap()
260 } else {
261 &self.groups
262 };
263 POOL.install(|| {
264 self.selected_keys
265 .par_iter()
266 .map(Column::as_materialized_series)
267 .map(|s| {
268 match groups {
269 GroupsType::Idx(groups) => {
270 let mut out = unsafe { s.take_slice_unchecked(groups.first()) };
272 if groups.sorted {
273 out.set_sorted_flag(s.is_sorted_flag());
274 };
275 out
276 },
277 GroupsType::Slice { groups, rolling } => {
278 if *rolling && !groups.is_empty() {
279 let offset = groups[0][0];
281 let [upper_offset, upper_len] = groups[groups.len() - 1];
282 return s.slice(
283 offset as i64,
284 ((upper_offset + upper_len) - offset) as usize,
285 );
286 }
287
288 let indices = groups
289 .iter()
290 .map(|&[first, _len]| first)
291 .collect_ca(PlSmallStr::EMPTY);
292 let mut out = unsafe { s.take_unchecked(&indices) };
294 out.set_sorted_flag(s.is_sorted_flag());
296 out
297 },
298 }
299 })
300 .map(Column::from)
301 .collect()
302 })
303 }
304
305 pub fn keys(&self) -> Vec<Column> {
306 self.keys_sliced(None)
307 }
308
309 fn prepare_agg(&self) -> PolarsResult<(Vec<Column>, Vec<Column>)> {
310 let keys = self.keys();
311
312 let agg_col = match &self.selected_agg {
313 Some(selection) => self.df.select_columns_impl(selection.as_slice()),
314 None => {
315 let by: Vec<_> = self.selected_keys.iter().map(|s| s.name()).collect();
316 let selection = self
317 .df
318 .iter()
319 .map(|s| s.name())
320 .filter(|a| !by.contains(a))
321 .cloned()
322 .collect::<Vec<_>>();
323
324 self.df.select_columns_impl(selection.as_slice())
325 },
326 }?;
327
328 Ok((keys, agg_col))
329 }
330
331 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
357 pub fn mean(&self) -> PolarsResult<DataFrame> {
358 let (mut cols, agg_cols) = self.prepare_agg()?;
359
360 for agg_col in agg_cols {
361 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Mean);
362 let mut agg = unsafe { agg_col.agg_mean(&self.groups) };
363 agg.rename(new_name);
364 cols.push(agg);
365 }
366 DataFrame::new(cols)
367 }
368
369 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
395 pub fn sum(&self) -> PolarsResult<DataFrame> {
396 let (mut cols, agg_cols) = self.prepare_agg()?;
397
398 for agg_col in agg_cols {
399 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Sum);
400 let mut agg = unsafe { agg_col.agg_sum(&self.groups) };
401 agg.rename(new_name);
402 cols.push(agg);
403 }
404 DataFrame::new(cols)
405 }
406
407 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
433 pub fn min(&self) -> PolarsResult<DataFrame> {
434 let (mut cols, agg_cols) = self.prepare_agg()?;
435 for agg_col in agg_cols {
436 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Min);
437 let mut agg = unsafe { agg_col.agg_min(&self.groups) };
438 agg.rename(new_name);
439 cols.push(agg);
440 }
441 DataFrame::new(cols)
442 }
443
444 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
470 pub fn max(&self) -> PolarsResult<DataFrame> {
471 let (mut cols, agg_cols) = self.prepare_agg()?;
472 for agg_col in agg_cols {
473 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Max);
474 let mut agg = unsafe { agg_col.agg_max(&self.groups) };
475 agg.rename(new_name);
476 cols.push(agg);
477 }
478 DataFrame::new(cols)
479 }
480
481 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
507 pub fn first(&self) -> PolarsResult<DataFrame> {
508 let (mut cols, agg_cols) = self.prepare_agg()?;
509 for agg_col in agg_cols {
510 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::First);
511 let mut agg = unsafe { agg_col.agg_first(&self.groups) };
512 agg.rename(new_name);
513 cols.push(agg);
514 }
515 DataFrame::new(cols)
516 }
517
518 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
544 pub fn last(&self) -> PolarsResult<DataFrame> {
545 let (mut cols, agg_cols) = self.prepare_agg()?;
546 for agg_col in agg_cols {
547 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Last);
548 let mut agg = unsafe { agg_col.agg_last(&self.groups) };
549 agg.rename(new_name);
550 cols.push(agg);
551 }
552 DataFrame::new(cols)
553 }
554
555 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
581 pub fn n_unique(&self) -> PolarsResult<DataFrame> {
582 let (mut cols, agg_cols) = self.prepare_agg()?;
583 for agg_col in agg_cols {
584 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::NUnique);
585 let mut agg = unsafe { agg_col.agg_n_unique(&self.groups) };
586 agg.rename(new_name);
587 cols.push(agg);
588 }
589 DataFrame::new(cols)
590 }
591
592 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
604 pub fn quantile(&self, quantile: f64, method: QuantileMethod) -> PolarsResult<DataFrame> {
605 polars_ensure!(
606 (0.0..=1.0).contains(&quantile),
607 ComputeError: "`quantile` should be within 0.0 and 1.0"
608 );
609 let (mut cols, agg_cols) = self.prepare_agg()?;
610 for agg_col in agg_cols {
611 let new_name = fmt_group_by_column(
612 agg_col.name().as_str(),
613 GroupByMethod::Quantile(quantile, method),
614 );
615 let mut agg = unsafe { agg_col.agg_quantile(&self.groups, quantile, method) };
616 agg.rename(new_name);
617 cols.push(agg);
618 }
619 DataFrame::new(cols)
620 }
621
622 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
633 pub fn median(&self) -> PolarsResult<DataFrame> {
634 let (mut cols, agg_cols) = self.prepare_agg()?;
635 for agg_col in agg_cols {
636 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Median);
637 let mut agg = unsafe { agg_col.agg_median(&self.groups) };
638 agg.rename(new_name);
639 cols.push(agg);
640 }
641 DataFrame::new(cols)
642 }
643
644 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
646 pub fn var(&self, ddof: u8) -> PolarsResult<DataFrame> {
647 let (mut cols, agg_cols) = self.prepare_agg()?;
648 for agg_col in agg_cols {
649 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Var(ddof));
650 let mut agg = unsafe { agg_col.agg_var(&self.groups, ddof) };
651 agg.rename(new_name);
652 cols.push(agg);
653 }
654 DataFrame::new(cols)
655 }
656
657 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
659 pub fn std(&self, ddof: u8) -> PolarsResult<DataFrame> {
660 let (mut cols, agg_cols) = self.prepare_agg()?;
661 for agg_col in agg_cols {
662 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Std(ddof));
663 let mut agg = unsafe { agg_col.agg_std(&self.groups, ddof) };
664 agg.rename(new_name);
665 cols.push(agg);
666 }
667 DataFrame::new(cols)
668 }
669
670 pub fn count(&self) -> PolarsResult<DataFrame> {
696 let (mut cols, agg_cols) = self.prepare_agg()?;
697
698 for agg_col in agg_cols {
699 let new_name = fmt_group_by_column(
700 agg_col.name().as_str(),
701 GroupByMethod::Count {
702 include_nulls: true,
703 },
704 );
705 let mut ca = self.groups.group_count();
706 ca.rename(new_name);
707 cols.push(ca.into_column());
708 }
709 DataFrame::new(cols)
710 }
711
712 pub fn groups(&self) -> PolarsResult<DataFrame> {
738 let mut cols = self.keys();
739 let mut column = self.groups.as_list_chunked();
740 let new_name = fmt_group_by_column("", GroupByMethod::Groups);
741 column.rename(new_name);
742 cols.push(column.into_column());
743 DataFrame::new(cols)
744 }
745
746 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
773 pub fn agg_list(&self) -> PolarsResult<DataFrame> {
774 let (mut cols, agg_cols) = self.prepare_agg()?;
775 for agg_col in agg_cols {
776 let new_name = fmt_group_by_column(agg_col.name().as_str(), GroupByMethod::Implode);
777 let mut agg = unsafe { agg_col.agg_list(&self.groups) };
778 agg.rename(new_name);
779 cols.push(agg);
780 }
781 DataFrame::new(cols)
782 }
783
784 fn prepare_apply(&self) -> PolarsResult<DataFrame> {
785 polars_ensure!(self.df.height() > 0, ComputeError: "cannot group_by + apply on empty 'DataFrame'");
786 if let Some(agg) = &self.selected_agg {
787 if agg.is_empty() {
788 Ok(self.df.clone())
789 } else {
790 let mut new_cols = Vec::with_capacity(self.selected_keys.len() + agg.len());
791 new_cols.extend_from_slice(&self.selected_keys);
792 let cols = self.df.select_columns_impl(agg.as_slice())?;
793 new_cols.extend(cols);
794 Ok(unsafe { DataFrame::new_no_checks(self.df.height(), new_cols) })
795 }
796 } else {
797 Ok(self.df.clone())
798 }
799 }
800
801 #[deprecated(since = "0.24.1", note = "use polars.lazy aggregations")]
803 pub fn par_apply<F>(&self, f: F) -> PolarsResult<DataFrame>
804 where
805 F: Fn(DataFrame) -> PolarsResult<DataFrame> + Send + Sync,
806 {
807 let df = self.prepare_apply()?;
808 let dfs = self
809 .get_groups()
810 .par_iter()
811 .map(|g| {
812 let sub_df = unsafe { take_df(&df, g) };
815 f(sub_df)
816 })
817 .collect::<PolarsResult<Vec<_>>>()?;
818
819 let mut df = accumulate_dataframes_vertical(dfs)?;
820 df.as_single_chunk_par();
821 Ok(df)
822 }
823
824 pub fn apply<F>(&self, mut f: F) -> PolarsResult<DataFrame>
826 where
827 F: FnMut(DataFrame) -> PolarsResult<DataFrame> + Send + Sync,
828 {
829 let df = self.prepare_apply()?;
830 let dfs = self
831 .get_groups()
832 .iter()
833 .map(|g| {
834 let sub_df = unsafe { take_df(&df, g) };
837 f(sub_df)
838 })
839 .collect::<PolarsResult<Vec<_>>>()?;
840
841 let mut df = accumulate_dataframes_vertical(dfs)?;
842 df.as_single_chunk_par();
843 Ok(df)
844 }
845
846 pub fn sliced(mut self, slice: Option<(i64, usize)>) -> Self {
847 match slice {
848 None => self,
849 Some((offset, length)) => {
850 self.groups = self.groups.slice(offset, length);
851 self.selected_keys = self.keys_sliced(slice);
852 self
853 },
854 }
855 }
856}
857
858unsafe fn take_df(df: &DataFrame, g: GroupsIndicator) -> DataFrame {
859 match g {
860 GroupsIndicator::Idx(idx) => df.take_slice_unchecked(idx.1),
861 GroupsIndicator::Slice([first, len]) => df.slice(first as i64, len as usize),
862 }
863}
864
865#[derive(Copy, Clone, Debug)]
866pub enum GroupByMethod {
867 Min,
868 NanMin,
869 Max,
870 NanMax,
871 Median,
872 Mean,
873 First,
874 Last,
875 Sum,
876 Groups,
877 NUnique,
878 Quantile(f64, QuantileMethod),
879 Count { include_nulls: bool },
880 Implode,
881 Std(u8),
882 Var(u8),
883}
884
885impl Display for GroupByMethod {
886 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
887 use GroupByMethod::*;
888 let s = match self {
889 Min => "min",
890 NanMin => "nan_min",
891 Max => "max",
892 NanMax => "nan_max",
893 Median => "median",
894 Mean => "mean",
895 First => "first",
896 Last => "last",
897 Sum => "sum",
898 Groups => "groups",
899 NUnique => "n_unique",
900 Quantile(_, _) => "quantile",
901 Count { .. } => "count",
902 Implode => "list",
903 Std(_) => "std",
904 Var(_) => "var",
905 };
906 write!(f, "{s}")
907 }
908}
909
910pub fn fmt_group_by_column(name: &str, method: GroupByMethod) -> PlSmallStr {
912 use GroupByMethod::*;
913 match method {
914 Min => format_pl_smallstr!("{name}_min"),
915 Max => format_pl_smallstr!("{name}_max"),
916 NanMin => format_pl_smallstr!("{name}_nan_min"),
917 NanMax => format_pl_smallstr!("{name}_nan_max"),
918 Median => format_pl_smallstr!("{name}_median"),
919 Mean => format_pl_smallstr!("{name}_mean"),
920 First => format_pl_smallstr!("{name}_first"),
921 Last => format_pl_smallstr!("{name}_last"),
922 Sum => format_pl_smallstr!("{name}_sum"),
923 Groups => PlSmallStr::from_static("groups"),
924 NUnique => format_pl_smallstr!("{name}_n_unique"),
925 Count { .. } => format_pl_smallstr!("{name}_count"),
926 Implode => format_pl_smallstr!("{name}_agg_list"),
927 Quantile(quantile, _interpol) => format_pl_smallstr!("{name}_quantile_{quantile:.2}"),
928 Std(_) => format_pl_smallstr!("{name}_agg_std"),
929 Var(_) => format_pl_smallstr!("{name}_agg_var"),
930 }
931}
932
933#[cfg(test)]
934mod test {
935 use num_traits::FloatConst;
936
937 use crate::prelude::*;
938
939 #[test]
940 #[cfg(feature = "dtype-date")]
941 #[cfg_attr(miri, ignore)]
942 fn test_group_by() -> PolarsResult<()> {
943 let s0 = Column::new(
944 PlSmallStr::from_static("date"),
945 &[
946 "2020-08-21",
947 "2020-08-21",
948 "2020-08-22",
949 "2020-08-23",
950 "2020-08-22",
951 ],
952 );
953 let s1 = Column::new(PlSmallStr::from_static("temp"), [20, 10, 7, 9, 1]);
954 let s2 = Column::new(PlSmallStr::from_static("rain"), [0.2, 0.1, 0.3, 0.1, 0.01]);
955 let df = DataFrame::new(vec![s0, s1, s2]).unwrap();
956
957 let out = df.group_by_stable(["date"])?.select(["temp"]).count()?;
958 assert_eq!(
959 out.column("temp_count")?,
960 &Column::new(PlSmallStr::from_static("temp_count"), [2 as IdxSize, 2, 1])
961 );
962
963 #[allow(deprecated)]
965 let out = df
967 .group_by_stable(["date"])?
968 .select(["temp", "rain"])
969 .mean()?;
970 assert_eq!(
971 out.column("temp_mean")?,
972 &Column::new(PlSmallStr::from_static("temp_mean"), [15.0f64, 4.0, 9.0])
973 );
974
975 #[allow(deprecated)]
977 let out = df
979 .group_by_stable(["date", "temp"])?
980 .select(["rain"])
981 .mean()?;
982 assert!(out.column("rain_mean").is_ok());
983
984 #[allow(deprecated)]
986 let out = df.group_by_stable(["date"])?.select(["temp"]).sum()?;
987 assert_eq!(
988 out.column("temp_sum")?,
989 &Column::new(PlSmallStr::from_static("temp_sum"), [30, 8, 9])
990 );
991
992 #[allow(deprecated)]
994 let gb = df.group_by(["date"]).unwrap().n_unique().unwrap();
996 assert_eq!(gb.width(), 3);
998 Ok(())
999 }
1000
1001 #[test]
1002 #[cfg_attr(miri, ignore)]
1003 fn test_static_group_by_by_12_columns() {
1004 let s0 = Column::new("G1".into(), ["A", "A", "B", "B", "C"].as_ref());
1006 let s1 = Column::new("N".into(), [1, 2, 2, 4, 2].as_ref());
1007 let s2 = Column::new("G2".into(), ["k", "l", "m", "m", "l"].as_ref());
1008 let s3 = Column::new("G3".into(), ["a", "b", "c", "c", "d"].as_ref());
1009 let s4 = Column::new("G4".into(), ["1", "2", "3", "3", "4"].as_ref());
1010 let s5 = Column::new("G5".into(), ["X", "Y", "Z", "Z", "W"].as_ref());
1011 let s6 = Column::new("G6".into(), [false, true, true, true, false].as_ref());
1012 let s7 = Column::new("G7".into(), ["r", "x", "q", "q", "o"].as_ref());
1013 let s8 = Column::new("G8".into(), ["R", "X", "Q", "Q", "O"].as_ref());
1014 let s9 = Column::new("G9".into(), [1, 2, 3, 3, 4].as_ref());
1015 let s10 = Column::new("G10".into(), [".", "!", "?", "?", "/"].as_ref());
1016 let s11 = Column::new("G11".into(), ["(", ")", "@", "@", "$"].as_ref());
1017 let s12 = Column::new("G12".into(), ["-", "_", ";", ";", ","].as_ref());
1018
1019 let df =
1020 DataFrame::new(vec![s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12]).unwrap();
1021
1022 #[allow(deprecated)]
1024 let adf = df
1025 .group_by([
1026 "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9", "G10", "G11", "G12",
1027 ])
1028 .unwrap()
1029 .select(["N"])
1030 .sum()
1031 .unwrap();
1032
1033 assert_eq!(
1034 Vec::from(&adf.column("N_sum").unwrap().i32().unwrap().sort(false)),
1035 &[Some(1), Some(2), Some(2), Some(6)]
1036 );
1037 }
1038
1039 #[test]
1040 #[cfg_attr(miri, ignore)]
1041 fn test_dynamic_group_by_by_13_columns() {
1042 let series_content = ["A", "A", "B", "B", "C"];
1044
1045 let series_names = [
1047 "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9", "G10", "G11", "G12", "G13",
1048 ];
1049
1050 let mut columns = Vec::with_capacity(14);
1052
1053 for series_name in series_names {
1055 let group_columns = Column::new(series_name.into(), series_content.as_ref());
1056 columns.push(group_columns);
1057 }
1058
1059 let agg_series = Column::new("N".into(), [1, 2, 3, 3, 4].as_ref());
1061 columns.push(agg_series);
1062
1063 let df = DataFrame::new(columns).unwrap();
1065
1066 #[allow(deprecated)]
1068 let adf = df
1070 .group_by(series_names)
1071 .unwrap()
1072 .select(["N"])
1073 .sum()
1074 .unwrap();
1075
1076 for series_name in &series_names {
1079 assert_eq!(
1080 Vec::from(&adf.column(series_name).unwrap().str().unwrap().sort(false)),
1081 &[Some("A"), Some("B"), Some("C")]
1082 );
1083 }
1084
1085 assert_eq!(
1087 Vec::from(&adf.column("N_sum").unwrap().i32().unwrap().sort(false)),
1088 &[Some(3), Some(4), Some(6)]
1089 );
1090 }
1091
1092 #[test]
1093 #[cfg_attr(miri, ignore)]
1094 fn test_group_by_floats() {
1095 let df = df! {"flt" => [1., 1., 2., 2., 3.],
1096 "val" => [1, 1, 1, 1, 1]
1097 }
1098 .unwrap();
1099 #[allow(deprecated)]
1101 let res = df.group_by(["flt"]).unwrap().sum().unwrap();
1102 let res = res.sort(["flt"], SortMultipleOptions::default()).unwrap();
1103 assert_eq!(
1104 Vec::from(res.column("val_sum").unwrap().i32().unwrap()),
1105 &[Some(2), Some(2), Some(1)]
1106 );
1107 }
1108
1109 #[test]
1110 #[cfg_attr(miri, ignore)]
1111 #[cfg(feature = "dtype-categorical")]
1112 fn test_group_by_categorical() {
1113 let mut df = df! {"foo" => ["a", "a", "b", "b", "c"],
1114 "ham" => ["a", "a", "b", "b", "c"],
1115 "bar" => [1, 1, 1, 1, 1]
1116 }
1117 .unwrap();
1118
1119 df.apply("foo", |s| {
1120 s.cast(&DataType::from_categories(Categories::global()))
1121 .unwrap()
1122 })
1123 .unwrap();
1124
1125 #[allow(deprecated)]
1127 let res = df
1129 .group_by_stable(["foo", "ham"])
1130 .unwrap()
1131 .select(["bar"])
1132 .sum()
1133 .unwrap();
1134
1135 assert_eq!(
1136 Vec::from(
1137 res.column("bar_sum")
1138 .unwrap()
1139 .as_materialized_series()
1140 .i32()
1141 .unwrap()
1142 ),
1143 &[Some(2), Some(2), Some(1)]
1144 );
1145 }
1146
1147 #[test]
1148 #[cfg_attr(miri, ignore)]
1149 fn test_group_by_null_handling() -> PolarsResult<()> {
1150 let df = df!(
1151 "a" => ["a", "a", "a", "b", "b"],
1152 "b" => [Some(1), Some(2), None, None, Some(1)]
1153 )?;
1154 #[allow(deprecated)]
1156 let out = df.group_by_stable(["a"])?.mean()?;
1157
1158 assert_eq!(
1159 Vec::from(out.column("b_mean")?.as_materialized_series().f64()?),
1160 &[Some(1.5), Some(1.0)]
1161 );
1162 Ok(())
1163 }
1164
1165 #[test]
1166 #[cfg_attr(miri, ignore)]
1167 fn test_group_by_var() -> PolarsResult<()> {
1168 let df = df![
1170 "g" => ["foo", "foo", "bar"],
1171 "flt" => [1.0, 2.0, 3.0],
1172 "int" => [1, 2, 3]
1173 ]?;
1174
1175 #[allow(deprecated)]
1177 let out = df.group_by_stable(["g"])?.select(["int"]).var(1)?;
1178
1179 assert_eq!(out.column("int_agg_var")?.f64()?.get(0), Some(0.5));
1180 #[allow(deprecated)]
1182 let out = df.group_by_stable(["g"])?.select(["int"]).std(1)?;
1183 let val = out.column("int_agg_std")?.f64()?.get(0).unwrap();
1184 let expected = f64::FRAC_1_SQRT_2();
1185 assert!((val - expected).abs() < 0.000001);
1186 Ok(())
1187 }
1188
1189 #[test]
1190 #[cfg_attr(miri, ignore)]
1191 #[cfg(feature = "dtype-categorical")]
1192 fn test_group_by_null_group() -> PolarsResult<()> {
1193 let mut df = df![
1195 "g" => [Some("foo"), Some("foo"), Some("bar"), None, None],
1196 "flt" => [1.0, 2.0, 3.0, 1.0, 1.0],
1197 "int" => [1, 2, 3, 1, 1]
1198 ]?;
1199
1200 df.try_apply("g", |s| {
1201 s.cast(&DataType::from_categories(Categories::global()))
1202 })?;
1203
1204 #[allow(deprecated)]
1206 let _ = df.group_by(["g"])?.sum()?;
1207 Ok(())
1208 }
1209}