@@ -120,7 +120,7 @@ export class GroupBy {
120120 throw new Error ( `Col_name must be an array of column` )
121121 }
122122
123-
123+ this . group_col_name = col_names // store the column name
124124 if ( this . key_col . length == 2 ) {
125125
126126 this . group_col = { }
@@ -165,13 +165,20 @@ export class GroupBy {
165165 */
166166 arithemetic ( operation ) {
167167
168- let ops_name = [ "mean" , "sum" , "count" , "mode" ]
168+ let ops_name = [ "mean" , "sum" , "count" , "mode" , "std" , "var" , "cumsum" , "cumprod" ,
169+ "cummax" , "cummin" ]
169170
170171 let ops_map = {
171172 "mean" : "mean()" ,
172173 "sum" : "sum()" ,
173174 "mode" : "mode()" ,
174- "count" : "count()"
175+ "count" : "count()" ,
176+ "std" : "std()" ,
177+ "var" : "var()" ,
178+ "cumsum" : "cumsum().values" ,
179+ "cumprod" : "cumprod().values" ,
180+ "cummax" : "cummax().values" ,
181+ "cummin" : "cummin().values"
175182 }
176183 let is_array = false ;
177184
@@ -201,8 +208,11 @@ export class GroupBy {
201208 } else {
202209 data = eval ( `this.group_col[key1][key2][i].${ operation } ` )
203210 }
204-
205- count_group [ key1 ] [ key2 ] . push ( data )
211+ if ( Array . isArray ( data ) ) {
212+ count_group [ key1 ] [ key2 ] . push ( ...data )
213+ } else {
214+ count_group [ key1 ] [ key2 ] . push ( data )
215+ }
206216 }
207217
208218 }
@@ -227,8 +237,12 @@ export class GroupBy {
227237 } else {
228238 data = eval ( `this.group_col[key1][i].${ operation } ` )
229239 }
240+ if ( Array . isArray ( data ) ) {
241+ count_group [ key1 ] . push ( ...data )
242+ } else {
243+ count_group [ key1 ] . push ( data )
244+ }
230245
231- count_group [ key1 ] . push ( data )
232246 }
233247 }
234248
@@ -241,15 +255,56 @@ export class GroupBy {
241255 count ( ) {
242256
243257 let value = this . arithemetic ( "count()" ) ;
244- return value ;
258+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "count" )
259+ return df
245260 }
246261
247262 sum ( ) {
248263 let value = this . arithemetic ( "sum()" )
249- return value
264+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "sum" )
265+ return df
266+ }
267+
268+ std ( ) {
269+ let value = this . arithemetic ( "std()" )
270+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "std" )
271+ return df
272+ }
273+
274+ var ( ) {
275+ let value = this . arithemetic ( "var()" )
276+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "var" )
277+ return df
250278 }
251279
280+ mean ( ) {
281+ let value = this . arithemetic ( "mean()" )
282+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "mean" )
283+ return df
284+ }
252285
286+ cumsum ( ) {
287+ let value = this . arithemetic ( "cumsum().values" )
288+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cumsum" )
289+ return df
290+ }
291+ cummax ( ) {
292+ let value = this . arithemetic ( "cummax().values" )
293+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cummax" )
294+ return df
295+ }
296+
297+ cumprod ( ) {
298+ let value = this . arithemetic ( "cumprod().values" )
299+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cumprod" )
300+ return df
301+ }
302+
303+ cummin ( ) {
304+ let value = this . arithemetic ( "cummin().values" )
305+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cummin" )
306+ return df
307+ }
253308
254309 /**
255310 * returns dataframe of a group
@@ -293,8 +348,63 @@ export class GroupBy {
293348 this . col ( columns )
294349
295350 let data = this . arithemetic ( operations )
351+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , data , operations )
352+
353+ return df ;
354+ }
296355
297- return data ;
356+ to_DataFrame ( key_col , col , data , ops ) {
357+
358+ if ( key_col . length == 2 ) {
359+
360+ let df_data = [ ]
361+ for ( let key_1 in data ) {
362+
363+ let key_val = data [ key_1 ]
364+
365+ for ( let key_2 in key_val ) {
366+ let k_data = key_val [ key_2 ]
367+ let kk = [ ]
368+ kk [ 0 ] = isNaN ( parseInt ( key_1 ) ) ? key_1 : parseInt ( key_1 )
369+ kk [ 1 ] = isNaN ( parseInt ( key_2 ) ) ? key_1 : parseInt ( key_2 )
370+ kk . push ( ...k_data )
371+ df_data . push ( kk )
372+
373+ }
374+
375+ }
376+ let column = [ ...key_col ]
377+
378+ let group_col = col . slice ( ) . map ( ( x , i ) => {
379+ if ( Array . isArray ( ops ) ) {
380+ return `${ x } _${ ops [ i ] } `
381+ }
382+ return `${ x } _${ ops } `
383+ } ) ;
384+ column . push ( ...group_col ) ;
385+ return new DataFrame ( df_data , { columns : column } )
386+ } else {
387+ let df_data = [ ]
388+ for ( let key_1 in data ) {
389+
390+ let key_val = data [ key_1 ]
391+
392+ let key_data = [ ]
393+ key_data [ 0 ] = isNaN ( parseInt ( key_1 ) ) ? key_1 : parseInt ( key_1 )
394+ key_data . push ( ...key_val )
395+ df_data . push ( key_data )
396+ }
397+ let column = [ ...key_col ]
398+ let group_col = col . slice ( ) . map ( ( x , i ) => {
399+ if ( ops . length > 1 ) {
400+ return `${ x } _${ ops [ i ] } `
401+ }
402+ return `${ x } _${ ops } `
403+ } ) ;
404+ column . push ( ...group_col ) ;
405+
406+ return new DataFrame ( df_data , { columns : column } )
407+ }
298408 }
299409
300410}
0 commit comments