table::group
On this page
Group-by and aggregation. Rows get a group id from a hash of their key
codes (keys); every aggregation is then one pass over its column,
indexing per-group accumulators by group id. Groups keep the order of
their first row. Above PAR_ROWS rows the grouping is partitioned by
hash and the partitions are grouped on several workers.
Structs
AggSource
pub struct AggOne aggregation: a kind, its input column and the output name
(<column>_<kind> unless named).
use table::{count, mean, sum};
let t = table::table!{ "g": ["a", "b", "a"], "x": [1.0, 2.0, 5.0] };
let s = try (try t.group_by(["g"])).agg([count(), sum("x"), mean("x").named("avg")]) catch _ { t };
assert(s.names() == ["g", "count", "x_sum", "avg"]);
assert(s.row(0).f64("avg") == 3.0);
The same aggregation with output column
name.
Trait implementations 1
impl Clone for Agg
GroupBySource
pub struct GroupByA table split into groups by key columns, ready to aggregate.
The number of groups.
Group id of each row (
0..len()).
Sourcepub fn agg(§self, aggs: List<Agg>) throws (TableError) -> Table One row per group: the key columns, then one column per aggregation in order.
The groups' sizes.
Enums
AggKindSource
pub enum AggKindWhat an aggregation computes.
Rows in the group (with a column: its non-null values).
Sample standard deviation (n - 1).
Sample variance (n - 1).
The
qquantile, linear between closest ranks (NumPy's default).The first non-null value.
The last non-null value.
Distinct non-null values.
A closure over the group's column.
Functions
countSource
pub fn count() -> Agg Rows per group, named count.
count_ofSource
pub fn count_of(c: str) -> Agg Non-null values of c per group.
sumSource
pub fn sum(c: str) -> Agg Sum (i64 stays i64; f64 with Neumaier compensation).
meanSource
pub fn mean(c: str) -> Agg Arithmetic mean, compensated sum over the count.
minSource
pub fn min(c: str) -> Agg Smallest value, any orderable type.
maxSource
pub fn max(c: str) -> Agg Largest value, any orderable type.
stdSource
pub fn std(c: str) -> Agg Sample standard deviation, Welford's one-pass update.
varSource
pub fn var(c: str) -> Agg Sample variance (n - 1), Welford.
medianSource
pub fn median(c: str) -> Agg The 0.5 quantile, linear interpolation.
quantileSource
pub fn quantile(c: str, q: f64) -> Agg The q quantile (0 <= q <= 1), linear interpolation.
firstSource
pub fn first(c: str) -> Agg The first non-null value.
lastSource
pub fn last(c: str) -> Agg The last non-null value.
n_uniqueSource
pub fn n_unique(c: str) -> Agg How many distinct non-null values.
customSource
pub fn custom(c: str, name: str, f: fn(Column) -> Value) -> Agg f of each group's slice of column c, named name.
use table::{custom, Value};
let t = table::table!{ "g": [1, 1, 2], "x": [3.0, 1.0, 7.0] };
let range = custom("x", "range", |c| {
let xs = try c.f64_list() catch _ { [0.0] };
Value::Float(xs.iter().fold(f64::NEG_INFINITY, |a, b| a.max(b)) - xs.iter().fold(f64::INFINITY, |a, b| a.min(b)))
});
let s = try (try t.group_by(["g"])).agg([range]) catch _ { t };
assert(s.row(0).f64("range") == 2.0);
Implementations
impl Table
Sourcepub fn group_by(§self, names: List<str>) throws (TableError) -> GroupBy Group by the columns
names(null is a key value of its own). Groups come out in the order of their first row.
Sourcepub fn agg(§self, aggs: List<Agg>) throws (TableError) -> Table Aggregations over the whole table, one row.
nabiuse table::{max, sum}; let t = table::table!{ "x": [1, 2, 3] }; let s = try t.agg([sum("x"), max("x")]) catch _ { t }; assert(s.row(0).i64("x_sum") == 6 && s.row(0).i64("x_max") == 3);