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+ }
0 commit comments