@@ -9,8 +9,6 @@ var _frame = require("./frame");
99
1010var _utils = require ( "./utils" ) ;
1111
12- var _concat = require ( "./concat.js" ) ;
13-
1412var _series = require ( "./series" ) ;
1513
1614const utils = new _utils . Utils ( ) ;
@@ -92,39 +90,38 @@ class GroupBy {
9290 throw new Error ( `Col_name must be an array of column` ) ;
9391 }
9492
95- this . group_col_name = col_names ;
93+ let group_col = { } ;
9694
9795 if ( this . key_col . length == 2 ) {
98- this . group_col = { } ;
99-
10096 for ( var key1 in this . data_tensors ) {
101- this . group_col [ key1 ] = { } ;
97+ group_col [ key1 ] = { } ;
10298
10399 for ( var key2 in this . data_tensors [ key1 ] ) {
104- this . group_col [ key1 ] [ key2 ] = [ ] ;
100+ group_col [ key1 ] [ key2 ] = [ ] ;
105101
106102 for ( let i = 0 ; i < col_names . length ; i ++ ) {
107103 let col_name = col_names [ i ] ;
108104 let data = this . data_tensors [ key1 ] [ key2 ] . column ( col_name ) ;
109- this . group_col [ key1 ] [ key2 ] . push ( data ) ;
105+ group_col [ key1 ] [ key2 ] . push ( data ) ;
110106 }
111107 }
112108 }
113109 } else {
114- this . group_col = { } ;
115-
116110 for ( let key1 in this . data_tensors ) {
117- this . group_col [ key1 ] = [ ] ;
111+ group_col [ key1 ] = [ ] ;
118112
119113 for ( let i = 0 ; i < col_names . length ; i ++ ) {
120114 let col_name = col_names [ i ] ;
121115 let data = this . data_tensors [ key1 ] . column ( col_name ) ;
122- this . group_col [ key1 ] . push ( data ) ;
116+ group_col [ key1 ] . push ( data ) ;
123117 }
124118 }
125119 }
126120
127- return this ;
121+ const gp = new GroupBy ( null , this . key_col , null , col_names ) ;
122+ gp . group_col = group_col ;
123+ gp . group_col_name = col_names ;
124+ return gp ;
128125 }
129126
130127 arithemetic ( operation ) {
@@ -206,70 +203,62 @@ class GroupBy {
206203 }
207204 }
208205
206+ operations ( ops , name ) {
207+ if ( ! this . group_col ) {
208+ let column = this . column_name . filter ( val => ! this . key_col . includes ( val ) ) ;
209+ let col_gp = this . col ( column ) ;
210+ let value = col_gp . arithemetic ( ops ) ;
211+ let df = col_gp . to_DataFrame ( col_gp . key_col , col_gp . group_col_name , value , name ) ;
212+ return df ;
213+ } else {
214+ let value = this . arithemetic ( ops ) ;
215+ let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , name ) ;
216+ return df ;
217+ }
218+ }
219+
209220 count ( ) {
210- let value = this . arithemetic ( "count()" ) ;
211- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "count" ) ;
212- return df ;
221+ return this . operations ( "count()" , "count" ) ;
213222 }
214223
215224 sum ( ) {
216- let value = this . arithemetic ( "sum()" ) ;
217- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "sum" ) ;
218- return df ;
225+ return this . operations ( "sum()" , "sum" ) ;
219226 }
220227
221228 std ( ) {
222- let value = this . arithemetic ( "std()" ) ;
223- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "std" ) ;
224- return df ;
229+ return this . operations ( "std()" , "std" ) ;
225230 }
226231
227232 var ( ) {
228- let value = this . arithemetic ( "var()" ) ;
229- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "var" ) ;
230- return df ;
233+ return this . operations ( "var()" , "var" ) ;
231234 }
232235
233236 mean ( ) {
234- let value = this . arithemetic ( "mean()" ) ;
235- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "mean" ) ;
236- return df ;
237+ return this . operations ( "mean()" , "mean" ) ;
237238 }
238239
239240 cumsum ( ) {
240- let value = this . arithemetic ( "cumsum().values" ) ;
241- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cumsum" ) ;
242- return df ;
241+ return this . operations ( "cumsum().values" , "cumsum" ) ;
243242 }
244243
245244 cummax ( ) {
246- let value = this . arithemetic ( "cummax().values" ) ;
247- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cummax" ) ;
248- return df ;
245+ return this . operations ( "cummax().values" , "cummax" ) ;
249246 }
250247
251248 cumprod ( ) {
252- let value = this . arithemetic ( "cumprod().values" ) ;
253- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cumprod" ) ;
254- return df ;
249+ return this . operations ( "cumprod().values" , "cumprod" ) ;
255250 }
256251
257252 cummin ( ) {
258- let value = this . arithemetic ( "cummin().values" ) ;
259- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "cummin" ) ;
260- return df ;
253+ return this . operations ( "cummin().values" , "cummin" ) ;
261254 }
262255
263256 max ( ) {
264- let value = this . arithemetic ( "max()" ) ;
265- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "max" ) ;
266- return df ;
257+ return this . operations ( "max()" , "max" ) ;
267258 }
268259
269260 min ( ) {
270- let value = this . arithemetic ( "min()" ) ;
271- let df = this . to_DataFrame ( this . key_col , this . group_col_name , value , "min" ) ;
272- return df ;
261+ return this . operations ( "min()" , "min" ) ;
273262 }
274263
275264 get_groups ( key ) {
@@ -302,9 +291,9 @@ class GroupBy {
302291 let operations = columns . map ( x => {
303292 return kwargs [ x ] . toLocaleLowerCase ( ) ;
304293 } ) ;
305- this . col ( columns ) ;
306- let data = this . arithemetic ( operations ) ;
307- let df = this . to_DataFrame ( this . key_col , this . group_col_name , data , operations ) ;
294+ let col_gp = this . col ( columns ) ;
295+ let data = col_gp . arithemetic ( operations ) ;
296+ let df = this . to_DataFrame ( col_gp . key_col , col_gp . group_col_name , data , operations ) ;
308297 return df ;
309298 }
310299
@@ -402,67 +391,65 @@ class GroupBy {
402391 }
403392 }
404393
405- apply ( kwargs ) {
406- let isCol ;
407- let column_names ;
394+ apply ( callable ) {
408395 let df_data ;
409- let callable = kwargs [ "callable" ] ;
396+ let column ;
410397
411- if ( kwargs [ "isCol" ] ) {
412- isCol = kwargs [ 'isCol' ] ;
398+ if ( ! this . group_col ) {
399+ column = this . column_name . filter ( val => ! this . key_col . includes ( val ) ) ;
400+ let col_gp = this . col ( column ) ;
401+ df_data = col_gp . group_col ;
413402 } else {
414- isCol = false ;
403+ column = this . group_col_name ;
404+ df_data = this . group_col ;
415405 }
416406
417407 let data = [ ] ;
418-
419- if ( isCol && this . group_col ) {
420- column_names = this . selected_column ;
421- df_data = this . group_col ;
422- console . log ( "here" ) ;
423- } else {
424- column_names = this . column_name ;
425- df_data = this . data_tensors ;
426- }
408+ let count_group = { } ;
427409
428410 if ( this . key_col . length == 2 ) {
429411 for ( let key in this . data_tensors ) {
412+ count_group [ key ] = { } ;
413+
430414 for ( let key2 in this . data_tensors [ key ] ) {
431- let callable_rslt = callable ( this . data_tensors [ key ] [ key2 ] ) ;
415+ count_group [ key ] [ key2 ] = [ ] ;
432416
433- if ( callable_rslt instanceof _frame . DataFrame ) {
434- data . push ( callable_rslt ) ;
435- } else {
436- data . push ( callable_rslt . values ) ;
417+ for ( let i = 0 ; i < df_data [ key ] [ key2 ] . length ; i ++ ) {
418+ let callable_rslt = callable ( df_data [ key ] [ key2 ] [ i ] ) ;
419+
420+ if ( callable_rslt instanceof _frame . DataFrame ) {
421+ count_group [ key ] [ key2 ] . push ( callable_rslt . values ) ;
422+ } else {
423+ if ( callable_rslt instanceof _series . Series ) {
424+ count_group [ key ] [ key2 ] . push ( callable_rslt . values ) ;
425+ } else {
426+ count_group [ key ] [ key2 ] . push ( callable_rslt ) ;
427+ }
428+ }
437429 }
438430 }
439431 }
440432 } else {
441433 for ( let key in df_data ) {
442- let callable_rslt = isCol ? callable ( df_data [ key ] [ 0 ] ) : callable ( df_data [ key ] ) ;
434+ count_group [ key ] = [ ] ;
443435
444- if ( callable_rslt instanceof _frame . DataFrame ) {
445- data . push ( callable_rslt ) ;
446- } else {
447- if ( Array . isArray ( callable_rslt . values ) ) {
448- data . push ( callable_rslt . values ) ;
436+ for ( let i = 0 ; i < df_data [ key ] . length ; i ++ ) {
437+ let callable_rslt = callable ( df_data [ key ] [ i ] ) ;
438+
439+ if ( callable_rslt instanceof _frame . DataFrame ) {
440+ count_group [ key ] . push ( callable_rslt . values ) ;
449441 } else {
450- data . push ( [ callable_rslt ] ) ;
442+ if ( callable_rslt instanceof _series . Series ) {
443+ count_group [ key ] . push ( callable_rslt . values ) ;
444+ } else {
445+ count_group [ key ] . push ( callable_rslt ) ;
446+ }
451447 }
452448 }
453449 }
454450 }
455451
456- if ( data [ 0 ] instanceof _frame . DataFrame ) {
457- return ( 0 , _concat . concat ) ( {
458- df_list : data ,
459- axis : 0
460- } ) ;
461- } else {
462- return new _frame . DataFrame ( data , {
463- columns : column_names
464- } ) ;
465- }
452+ return this . to_DataFrame ( this . key_col , column , count_group , "apply" ) ;
466453 }
467454
468455}
0 commit comments