Expand description
Group-level iteration over a DataFrame key column.
Two rungs, cheapest first:
-
Typed rows, grouped Rust-side — after
Vec::<Row>::from_dataframe(&df)?, grouping is plain Rust.group_rowsmakes the idiom discoverable:ⓘlet rows: Vec<Obs> = Vec::from_dataframe(&df)?; let by_site = group_rows(rows, |r| r.site.clone()); // by_site: BTreeMap<String, Vec<Obs>> — plain Rust data, rayon-safe. -
Untyped, index-based —
DataFrame::group_bycomputes group indices once (single pass, main thread) without extracting rows:ⓘlet grouped = df.group_by("site")?; for (key, rows) in grouped.iter() { /* key: &GroupKey, rows: &[usize] */ } let mut out = NamedDataFrameListBuilder::with_capacity(grouped.len()); for (key, sub) in grouped.frames() { out = out.push(key.label(), sub); }
§Key semantics (vs R split())
- Group order: factor keys follow level order (empty levels kept, like
split()); character keys sort in byte order (R sorts in locale collation order — identical for ASCII); integer keys sort numerically; logical keys orderFALSE,TRUE. NAkeys form one group, ordered last — a deliberate deviation fromsplit(), which silently drops NA-keyed rows. A literal NA level (addNA(f)) also surfaces asGroupKey::Na.- Double key columns are an error: grouping on floating point is a
footgun —
cut()orfactor()the column first.
Structs§
- Grouped
Data Frame - A
DataFramepartitioned by the values of one key column.
Enums§
- Group
Key - The key of one group produced by
DataFrame::group_by.
Functions§
- character_
groups 🔒 - Character keys: byte-order sort (BTreeMap), NA last.
- factor_
groups 🔒 - Factor fast path: levels are the keys (level order, empty levels kept).
NA codes — and a literal NA level from
addNA()— land inGroupKey::Na. - group_
rows - Group already-extracted rows by a key function.
- integer_
groups 🔒 - Integer keys: numeric sort (BTreeMap), NA (
i32::MIN) last. - logical_
groups 🔒 - Logical keys:
FALSEthenTRUE(R’s sort order), NA last. Only keys present in the data appear.