pub struct GroupBy<'a> {
pub df: &'a DataFrame,
/* private fields */
}Expand description
Returned by a group_by operation on a DataFrame. This struct supports several aggregations.
Until described otherwise, the examples in this struct are performed on the following DataFrame:
use polars_core::prelude::*;
let dates = &[
"2020-08-21",
"2020-08-21",
"2020-08-22",
"2020-08-23",
"2020-08-22",
];
// date format
let fmt = "%Y-%m-%d";
// create date series
let s0 = DateChunked::parse_from_str_slice("date", dates, fmt)
.into_series();
// create temperature series
let s1 = Series::new("temp".into(), [20, 10, 7, 9, 1]);
// create rain series
let s2 = Series::new("rain".into(), [0.2, 0.1, 0.3, 0.1, 0.01]);
// create a new DataFrame
let df = DataFrame::new_infer_height(vec![s0, s1, s2]).unwrap();
println!("{:?}", df);Outputs:
+------------+------+------+
| date | temp | rain |
| --- | --- | --- |
| Date | i32 | f64 |
+============+======+======+
| 2020-08-21 | 20 | 0.2 |
+------------+------+------+
| 2020-08-21 | 10 | 0.1 |
+------------+------+------+
| 2020-08-22 | 7 | 0.3 |
+------------+------+------+
| 2020-08-23 | 9 | 0.1 |
+------------+------+------+
| 2020-08-22 | 1 | 0.01 |
+------------+------+------+Fields§
§df: &'a DataFrameImplementations§
Source§impl<'a> GroupBy<'a>
impl<'a> GroupBy<'a>
pub fn new( df: &'a DataFrame, by: Vec<Column>, groups: GroupPositions, selected_agg: Option<Vec<PlSmallStr>>, ) -> GroupBy<'a>
Sourcepub fn select<I, S>(self, selection: I) -> GroupBy<'a>
pub fn select<I, S>(self, selection: I) -> GroupBy<'a>
Select the column(s) that should be aggregated. You can select a single column or a slice of columns.
Note that making a selection with this method is not required. If you skip it all columns (except for the keys) will be selected for aggregation.
Sourcepub fn get_groups(&self) -> &GroupPositions
pub fn get_groups(&self) -> &GroupPositions
Get the internal representation of the GroupBy operation.
The Vec returned contains:
(first_idx, Vec<indexes>)
Where second value in the tuple is a vector with all matching indexes.
pub fn into_groups(self) -> GroupPositions
pub fn keys_sliced(&self, slice: Option<(i64, usize)>) -> Vec<Column>
pub fn keys(&self) -> Vec<Column>
Sourcepub fn count(&self) -> Result<DataFrame, PolarsError>
pub fn count(&self) -> Result<DataFrame, PolarsError>
Aggregate grouped series and compute the number of values per group.
§Example
fn example(df: DataFrame) -> PolarsResult<DataFrame> {
df.group_by(["date"])?.select(["temp"]).count()
}Returns:
+------------+------------+
| date | temp_count |
| --- | --- |
| Date | u32 |
+============+============+
| 2020-08-23 | 1 |
+------------+------------+
| 2020-08-22 | 2 |
+------------+------------+
| 2020-08-21 | 2 |
+------------+------------+Sourcepub fn groups(&self) -> Result<DataFrame, PolarsError>
pub fn groups(&self) -> Result<DataFrame, PolarsError>
Get the group_by group indexes.
§Example
fn example(df: DataFrame) -> PolarsResult<DataFrame> {
df.group_by(["date"])?.groups()
}Returns:
+--------------+------------+
| date | groups |
| --- | --- |
| Date(days) | list [u32] |
+==============+============+
| 2020-08-23 | "[3]" |
+--------------+------------+
| 2020-08-22 | "[2, 4]" |
+--------------+------------+
| 2020-08-21 | "[0, 1]" |
+--------------+------------+Sourcepub fn apply<F>(&self, f: F) -> Result<DataFrame, PolarsError>
pub fn apply<F>(&self, f: F) -> Result<DataFrame, PolarsError>
Apply a closure over the groups as a new DataFrame.
pub fn apply_sliced<F>( &self, slice: Option<(i64, usize)>, f: F, schema: Option<&Arc<Schema<DataType, ()>>>, ) -> Result<DataFrame, PolarsError>
pub fn sliced(self, slice: Option<(i64, usize)>) -> GroupBy<'a>
Trait Implementations§
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for GroupBy<'a>
impl<'a> !UnwindSafe for GroupBy<'a>
impl<'a> Freeze for GroupBy<'a>
impl<'a> Send for GroupBy<'a>
impl<'a> Sync for GroupBy<'a>
impl<'a> Unpin for GroupBy<'a>
impl<'a> UnsafeUnpin for GroupBy<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more