packages

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 Agg

One aggregation: a kind, its input column and the output name (<column>_<kind> unless named).

nabi
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);
  • pub fn named(self, name: str) -> Agg§

    Source

    The same aggregation with output column name.

Trait implementations 1
  • impl Clone for Agg

GroupBySource

pub struct GroupBy

A table split into groups by key columns, ready to aggregate.

  • pub fn len(self) -> usize§

    Source

    The number of groups.

  • pub fn group_ids(self) -> List<u32>§

    Source

    Group id of each row (0..len()).

  • pub fn agg(self, aggs: List<Agg>) throws (TableError) -> Table§

    Source

    One row per group: the key columns, then one column per aggregation in order.

  • pub fn counts(self) -> List<i64>§

    Source

    The groups' sizes.

Enums

AggKindSource

pub enum AggKind

What an aggregation computes.

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.

nabi
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

  • pub fn group_by(self, names: List<str>) throws (TableError) -> GroupBy§

    Source

    Group by the columns names (null is a key value of its own). Groups come out in the order of their first row.

  • pub fn agg(self, aggs: List<Agg>) throws (TableError) -> Table§

    Source

    Aggregations over the whole table, one row.

    nabi
    use 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);