Skip to content

Commit 8345e01

Browse files
committed
re-implementing Groupby.js
1 parent d720988 commit 8345e01

2 files changed

Lines changed: 100 additions & 1 deletion

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Console } from "console";
2+
import DataFrame from "../core/frame"
3+
import { ArrayType1D, ArrayType2D } from "../shared/types"
4+
import Utils from "../shared/utils";
5+
import concat from "../transformers/concat"
6+
7+
8+
/**
9+
* The class performs all groupby operation on a dataframe
10+
* involving all aggregate funciton
11+
* @param {colDict} colDict Object of unique keys in the group by column
12+
* @param {keyCol} keyCol Array contains the column names
13+
* @param {data} Array the dataframe data
14+
* @param {columnName} Array of all column name in the dataframe.
15+
* @param {colDtype} Array columns dtype
16+
*/
17+
export default class Groupby {
18+
colDict?: { [key: string ]: {} }
19+
keyCol: ArrayType1D
20+
data?: ArrayType2D
21+
columnName: ArrayType1D
22+
colDtype: ArrayType1D
23+
colIndex: ArrayType1D
24+
groupDict?: any
25+
26+
constructor(keyCol: ArrayType1D, data: ArrayType2D, columnName: ArrayType1D, colDtype:ArrayType1D, colIndex: ArrayType1D) {
27+
28+
this.keyCol = keyCol;
29+
this.data = data;
30+
this.columnName = columnName;
31+
//this.dataTensors = {}; //store the tensor version of the groupby data
32+
this.colDtype = colDtype;
33+
this.colIndex = colIndex
34+
35+
}
36+
37+
group(): Groupby{
38+
this.groupDict = this.data?.reduce((prev, current)=>{
39+
function dfs(arr: ArrayType1D, value: ArrayType1D, obj: any) {
40+
let firstIndex = arr[0]
41+
let remainingndex = arr.slice(1)
42+
43+
if(!remainingndex.length) {
44+
value.forEach((el, i) => {
45+
el = String(el)
46+
if (i == firstIndex) {
47+
if (el in Object.keys(obj)) {
48+
obj[el].push(value)
49+
} else {
50+
obj[el] = [value]
51+
}
52+
}
53+
});
54+
} else {
55+
value.forEach((el, i) => {
56+
if (i == firstIndex) {
57+
el = String(el)
58+
if (el as string in obj) {
59+
obj[el] = dfs(remainingndex, value, obj[el])
60+
}
61+
else {
62+
obj[el] = dfs(remainingndex, value, {})
63+
}
64+
}
65+
})
66+
}
67+
return obj
68+
}
69+
70+
prev = dfs(this.colIndex, current, prev)
71+
return prev
72+
73+
}, {})
74+
75+
delete this.data
76+
return this
77+
}
78+
}

src/danfojs-base/core/frame.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Utils from "../shared/utils"
2424
import NDframe from "./generic";
2525
import { table } from "table";
2626
import Series from './series';
27-
27+
import Groupby from '../aggregators/groupby'
2828
const utils = new Utils();
2929

3030
/**
@@ -2418,4 +2418,25 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
24182418
}
24192419

24202420
}
2421+
2422+
/**
2423+
* Groupby
2424+
* @params col a list of column
2425+
*/
2426+
groupby(col: Array<string>): Groupby {
2427+
const columns = this.columns
2428+
const colIndex = col.map((val) => columns.indexOf(val))
2429+
const colDtype = this.dtypes.filter((val, index) => {
2430+
return colIndex.includes(index)
2431+
})
2432+
2433+
return new Groupby(
2434+
col,
2435+
this.values as ArrayType2D,
2436+
columns,
2437+
colDtype,
2438+
colIndex
2439+
).group()
2440+
2441+
}
24212442
}

0 commit comments

Comments
 (0)