|
| 1 | +// src/core/dataframe/GroupBy.js |
| 2 | +import { DataFrame } from './DataFrame.js'; |
| 3 | +import { Series } from './Series.js'; |
| 4 | + |
| 5 | +export class GroupBy { |
| 6 | + /** |
| 7 | + * @param {DataFrame} df - Source DataFrame |
| 8 | + * @param {string|string[]} by - Column(s) to group by |
| 9 | + */ |
| 10 | + constructor(df, by) { |
| 11 | + this.df = df; |
| 12 | + this.by = Array.isArray(by) ? by : [by]; |
| 13 | + this._groups = this._createGroups(); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Creates groups based on unique values in the grouping columns |
| 18 | + * @private |
| 19 | + * @returns {Map} - Map of group keys to row indices |
| 20 | + */ |
| 21 | + _createGroups() { |
| 22 | + const groups = new Map(); |
| 23 | + const rows = this.df.toArray(); |
| 24 | + |
| 25 | + // Group rows by the values in the 'by' columns |
| 26 | + for (let i = 0; i < rows.length; i++) { |
| 27 | + const row = rows[i]; |
| 28 | + const key = this.by.map((col) => row[col]).join('|'); |
| 29 | + |
| 30 | + if (!groups.has(key)) { |
| 31 | + groups.set(key, []); |
| 32 | + } |
| 33 | + |
| 34 | + groups.get(key).push(i); |
| 35 | + } |
| 36 | + |
| 37 | + return groups; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Applies an aggregation function to each group |
| 42 | + * @param {Object} aggregations - Map of column names to aggregation functions |
| 43 | + * @returns {DataFrame} - DataFrame with aggregated results |
| 44 | + */ |
| 45 | + agg(aggregations) { |
| 46 | + const result = {}; |
| 47 | + |
| 48 | + // Add grouping columns to result |
| 49 | + for (const col of this.by) { |
| 50 | + result[col] = []; |
| 51 | + } |
| 52 | + |
| 53 | + // Add aggregation columns to result |
| 54 | + for (const col in aggregations) { |
| 55 | + result[col] = []; |
| 56 | + } |
| 57 | + |
| 58 | + // Process each group |
| 59 | + for (const [key, indices] of this._groups.entries()) { |
| 60 | + // Extract group key values |
| 61 | + const keyValues = key.split('|'); |
| 62 | + |
| 63 | + // Add group key values to result |
| 64 | + for (let i = 0; i < this.by.length; i++) { |
| 65 | + result[this.by[i]].push(keyValues[i]); |
| 66 | + } |
| 67 | + |
| 68 | + // Create subset DataFrame for this group |
| 69 | + const groupRows = indices.map((idx) => this.df.toArray()[idx]); |
| 70 | + const groupDf = DataFrame.fromRows(groupRows); |
| 71 | + |
| 72 | + // Apply aggregations |
| 73 | + for (const col in aggregations) { |
| 74 | + const aggFunc = aggregations[col]; |
| 75 | + const aggValue = aggFunc(groupDf.col(col)); |
| 76 | + result[col].push(aggValue); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return new DataFrame(result); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Applies a function to each group and returns a DataFrame with the results |
| 85 | + * @param {Function} fn - Function to apply to each group |
| 86 | + * @returns {DataFrame} - DataFrame with transformed groups |
| 87 | + */ |
| 88 | + apply(fn) { |
| 89 | + const results = []; |
| 90 | + |
| 91 | + // Process each group |
| 92 | + for (const [key, indices] of this._groups.entries()) { |
| 93 | + // Create subset DataFrame for this group |
| 94 | + const groupRows = indices.map((idx) => this.df.toArray()[idx]); |
| 95 | + const groupDf = DataFrame.fromRows(groupRows); |
| 96 | + |
| 97 | + // Apply function to group |
| 98 | + const result = fn(groupDf); |
| 99 | + |
| 100 | + // Add group key information |
| 101 | + const keyValues = key.split('|'); |
| 102 | + for (let i = 0; i < this.by.length; i++) { |
| 103 | + result[this.by[i]] = keyValues[i]; |
| 104 | + } |
| 105 | + |
| 106 | + results.push(result); |
| 107 | + } |
| 108 | + |
| 109 | + return DataFrame.fromRows(results); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns the number of items in each group |
| 114 | + * @returns {DataFrame} - DataFrame with group counts |
| 115 | + */ |
| 116 | + count() { |
| 117 | + return this.agg({ |
| 118 | + count: (series) => series.length, |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns the sum of values in each group |
| 124 | + * @param {string} column - Column to sum |
| 125 | + * @returns {DataFrame} - DataFrame with group sums |
| 126 | + */ |
| 127 | + sum(column) { |
| 128 | + const agg = {}; |
| 129 | + agg[column] = (series) => series.sum(); |
| 130 | + return this.agg(agg); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns the mean of values in each group |
| 135 | + * @param {string} column - Column to average |
| 136 | + * @returns {DataFrame} - DataFrame with group means |
| 137 | + */ |
| 138 | + mean(column) { |
| 139 | + const agg = {}; |
| 140 | + agg[column] = (series) => series.mean(); |
| 141 | + return this.agg(agg); |
| 142 | + } |
| 143 | +} |
0 commit comments