11import { DataFrame } from "./frame" ;
22import { Utils } from "./utils" ;
3+ import { Series } from "./series" ;
34const utils = new Utils ;
45
56/**
@@ -101,7 +102,7 @@ export class GroupBy {
101102 * @return Groupby data structure
102103 */
103104 col ( col_names ) {
104-
105+ this . selected_column = col_names ; // store col_names for use later in .apply
105106 if ( Array . isArray ( col_names ) ) {
106107
107108 for ( let i = 0 ; i < col_names . length ; i ++ ) {
@@ -115,44 +116,50 @@ export class GroupBy {
115116 throw new Error ( `Col_name must be an array of column` ) ;
116117 }
117118
118- this . group_col_name = col_names ; // store the column name
119+ // let group_col_name = col_names; // store the column name
120+ let group_col = { } ;
119121 if ( this . key_col . length == 2 ) {
120122
121- this . group_col = { } ;
122-
123123 for ( var key1 in this . data_tensors ) {
124124
125- this . group_col [ key1 ] = { } ;
125+ group_col [ key1 ] = { } ;
126126 for ( var key2 in this . data_tensors [ key1 ] ) {
127127
128- this . group_col [ key1 ] [ key2 ] = [ ] ;
128+ group_col [ key1 ] [ key2 ] = [ ] ;
129129 for ( let i = 0 ; i < col_names . length ; i ++ ) {
130130 let col_name = col_names [ i ] ;
131131 let data = this . data_tensors [ key1 ] [ key2 ] . column ( col_name ) ;
132- this . group_col [ key1 ] [ key2 ] . push ( data ) ;
132+ group_col [ key1 ] [ key2 ] . push ( data ) ;
133133 }
134134
135135 }
136136 }
137137 } else {
138-
139- this . group_col = { } ;
140-
141138 for ( let key1 in this . data_tensors ) {
142139
143- this . group_col [ key1 ] = [ ] ;
140+ group_col [ key1 ] = [ ] ;
144141 for ( let i = 0 ; i < col_names . length ; i ++ ) {
145142 let col_name = col_names [ i ] ;
146143 let data = this . data_tensors [ key1 ] . column ( col_name ) ;
147- this . group_col [ key1 ] . push ( data ) ;
144+ group_col [ key1 ] . push ( data ) ;
148145 }
149146
150147 }
151148 }
152-
153- return this ;
149+ const gp = new GroupBy (
150+ null ,
151+ this . key_col ,
152+ null ,
153+ col_names
154+ ) ;
155+
156+ gp . group_col = group_col ;
157+ gp . group_col_name = col_names ;
158+ // return gp;
159+ return gp ;
154160 }
155161
162+
156163 /**
157164 * Basic root of all column arithemetic in groups
158165 * @param {operation } operatioin String
@@ -178,8 +185,8 @@ export class GroupBy {
178185 //the local variable to store variables to be used in eval
179186 // this seems not to be needed in Node version, since local
180187 //variable are easily accessed in the eval function
181- let local = null ;
182-
188+ let local = null ;
189+
183190 if ( Array . isArray ( operation ) ) {
184191 is_array = true ;
185192 }
@@ -248,70 +255,60 @@ export class GroupBy {
248255
249256 }
250257
258+ operations ( ops , name ) {
259+ if ( ! this . group_col ) {
260+ let column = this . column_name . filter ( ( val ) => ! this . key_col . includes ( val ) ) ;
261+ let col_gp = this . col ( column ) ;
262+ let value = col_gp . arithemetic ( ops ) ;
263+ let df = col_gp . to_DataFrame ( col_gp . key_col , col_gp . group_col_name , value , name ) ;
264+ return df ;
265+ } else {
266+ let value = this . arithemetic ( ops ) ;
267+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , name ) ;
268+ return df ;
269+ }
270+ }
251271 count ( ) {
252-
253- let value = this . arithemetic ( "count()" ) ;
254- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "count" ) ;
255- return df ;
272+ return this . operations ( "count()" , "count" ) ;
256273 }
257274
258275 sum ( ) {
259- let value = this . arithemetic ( "sum()" ) ;
260- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "sum" ) ;
261- return df ;
276+ return this . operations ( "sum()" , "sum" ) ;
262277 }
263278
264279 std ( ) {
265- let value = this . arithemetic ( "std()" ) ;
266- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "std" ) ;
267- return df ;
280+ return this . operations ( "std()" , "std" ) ;
268281 }
269282
270283 var ( ) {
271- let value = this . arithemetic ( "var()" ) ;
272- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "var" ) ;
273- return df ;
284+ return this . operations ( "var()" , "var" ) ;
274285 }
275286
276287 mean ( ) {
277- let value = this . arithemetic ( "mean()" ) ;
278- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "mean" ) ;
279- return df ;
288+ return this . operations ( "mean()" , "mean" ) ;
280289 }
281290
282291 cumsum ( ) {
283- let value = this . arithemetic ( "cumsum().values" ) ;
284- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cumsum" ) ;
285- return df ;
292+ return this . operations ( "cumsum().values" , "cumsum" ) ;
286293 }
287294 cummax ( ) {
288- let value = this . arithemetic ( "cummax().values" ) ;
289- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cummax" ) ;
290- return df ;
295+ return this . operations ( "cummax().values" , "cummax" ) ;
291296 }
292297
293298 cumprod ( ) {
294- let value = this . arithemetic ( "cumprod().values" ) ;
295- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cumprod" ) ;
296- return df ;
299+ return this . operations ( "cumprod().values" , "cumprod" ) ;
297300 }
298301
299302 cummin ( ) {
300- let value = this . arithemetic ( "cummin().values" ) ;
301- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cummin" ) ;
302- return df ;
303+ return this . operations ( "cummin().values" , "cummin" ) ;
303304 }
304305
305306 max ( ) {
306- let value = this . arithemetic ( "max()" ) ;
307- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "max" ) ;
308- return df ;
307+ return this . operations ( "max()" , "max" ) ;
309308 }
310309
311310 min ( ) {
312- let value = this . arithemetic ( "min()" ) ;
313- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "min" ) ;
314- return df ;
311+ return this . operations ( "min()" , "min" ) ;
315312 }
316313
317314 /**
@@ -350,17 +347,16 @@ export class GroupBy {
350347 let columns = Object . keys ( kwargs ) ;
351348 let operations = columns . map ( ( x ) => { return kwargs [ x ] . toLocaleLowerCase ( ) ; } ) ;
352349
353- this . col ( columns ) ;
350+ let col_gp = this . col ( columns ) ;
354351
355- let data = this . arithemetic ( operations ) ;
356- let df = this . to_DataFrame ( this . key_col , this . group_col_name , data , operations ) ;
352+ let data = col_gp . arithemetic ( operations ) ;
353+ let df = this . to_DataFrame ( col_gp . key_col , col_gp . group_col_name , data , operations ) ;
357354
358355 return df ;
359356 }
360357
361358 to_DataFrame ( key_col , col , data , ops ) {
362359
363- // console.log(data);
364360 if ( key_col . length == 2 ) {
365361 let df_data = [ ] ;
366362 for ( let key_1 in data ) {
@@ -455,4 +451,61 @@ export class GroupBy {
455451 }
456452 }
457453
454+ apply ( callable ) {
455+ let df_data ;
456+ let column ;
457+ if ( ! this . group_col ) {
458+ column = this . column_name . filter ( ( val ) => ! this . key_col . includes ( val ) ) ;
459+ let col_gp = this . col ( column ) ;
460+ df_data = col_gp . group_col ;
461+ } else {
462+ column = this . group_col_name ;
463+ df_data = this . group_col ;
464+ }
465+ let data = [ ] ;
466+ let count_group = { } ;
467+ if ( this . key_col . length == 2 ) {
468+
469+ for ( let key in this . data_tensors ) {
470+ count_group [ key ] = { } ;
471+ for ( let key2 in this . data_tensors [ key ] ) {
472+ count_group [ key ] [ key2 ] = [ ] ;
473+ for ( let i = 0 ; i < df_data [ key ] [ key2 ] . length ; i ++ ) {
474+ let callable_rslt = callable ( df_data [ key ] [ key2 ] [ i ] ) ;
475+ if ( callable_rslt instanceof DataFrame ) {
476+ count_group [ key ] [ key2 ] . push ( callable_rslt . values ) ;
477+ } else {
478+ if ( callable_rslt instanceof Series ) {
479+ count_group [ key ] [ key2 ] . push ( callable_rslt . values ) ;
480+ } else {
481+ count_group [ key ] [ key2 ] . push ( callable_rslt ) ;
482+ }
483+ }
484+ }
485+
486+
487+ }
488+ }
489+ } else {
490+ for ( let key in df_data ) {
491+ count_group [ key ] = [ ] ;
492+ for ( let i = 0 ; i < df_data [ key ] . length ; i ++ ) {
493+ let callable_rslt = callable ( df_data [ key ] [ i ] ) ;
494+ if ( callable_rslt instanceof DataFrame ) {
495+ count_group [ key ] . push ( callable_rslt . values ) ;
496+ } else {
497+ if ( callable_rslt instanceof Series ) {
498+ count_group [ key ] . push ( callable_rslt . values ) ;
499+ } else {
500+ count_group [ key ] . push ( callable_rslt ) ;
501+ }
502+
503+ }
504+ }
505+
506+ }
507+ }
508+ return this . to_DataFrame ( this . key_col , column , count_group , "apply" ) ;
509+ }
510+
458511}
0 commit comments