1
1
import { DataFrame } from "./frame" ;
2
2
import { Utils } from "./utils" ;
3
+ import { Series } from "./series" ;
3
4
const utils = new Utils ;
4
5
5
6
/**
@@ -101,7 +102,7 @@ export class GroupBy {
101
102
* @return Groupby data structure
102
103
*/
103
104
col ( col_names ) {
104
-
105
+ this . selected_column = col_names ; // store col_names for use later in .apply
105
106
if ( Array . isArray ( col_names ) ) {
106
107
107
108
for ( let i = 0 ; i < col_names . length ; i ++ ) {
@@ -115,44 +116,50 @@ export class GroupBy {
115
116
throw new Error ( `Col_name must be an array of column` ) ;
116
117
}
117
118
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 = { } ;
119
121
if ( this . key_col . length == 2 ) {
120
122
121
- this . group_col = { } ;
122
-
123
123
for ( var key1 in this . data_tensors ) {
124
124
125
- this . group_col [ key1 ] = { } ;
125
+ group_col [ key1 ] = { } ;
126
126
for ( var key2 in this . data_tensors [ key1 ] ) {
127
127
128
- this . group_col [ key1 ] [ key2 ] = [ ] ;
128
+ group_col [ key1 ] [ key2 ] = [ ] ;
129
129
for ( let i = 0 ; i < col_names . length ; i ++ ) {
130
130
let col_name = col_names [ i ] ;
131
131
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 ) ;
133
133
}
134
134
135
135
}
136
136
}
137
137
} else {
138
-
139
- this . group_col = { } ;
140
-
141
138
for ( let key1 in this . data_tensors ) {
142
139
143
- this . group_col [ key1 ] = [ ] ;
140
+ group_col [ key1 ] = [ ] ;
144
141
for ( let i = 0 ; i < col_names . length ; i ++ ) {
145
142
let col_name = col_names [ i ] ;
146
143
let data = this . data_tensors [ key1 ] . column ( col_name ) ;
147
- this . group_col [ key1 ] . push ( data ) ;
144
+ group_col [ key1 ] . push ( data ) ;
148
145
}
149
146
150
147
}
151
148
}
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 ;
154
160
}
155
161
162
+
156
163
/**
157
164
* Basic root of all column arithemetic in groups
158
165
* @param {operation } operatioin String
@@ -178,8 +185,8 @@ export class GroupBy {
178
185
//the local variable to store variables to be used in eval
179
186
// this seems not to be needed in Node version, since local
180
187
//variable are easily accessed in the eval function
181
- let local = null ;
182
-
188
+ let local = null ;
189
+
183
190
if ( Array . isArray ( operation ) ) {
184
191
is_array = true ;
185
192
}
@@ -248,70 +255,60 @@ export class GroupBy {
248
255
249
256
}
250
257
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
+ }
251
271
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" ) ;
256
273
}
257
274
258
275
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" ) ;
262
277
}
263
278
264
279
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" ) ;
268
281
}
269
282
270
283
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" ) ;
274
285
}
275
286
276
287
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" ) ;
280
289
}
281
290
282
291
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" ) ;
286
293
}
287
294
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" ) ;
291
296
}
292
297
293
298
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" ) ;
297
300
}
298
301
299
302
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" ) ;
303
304
}
304
305
305
306
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" ) ;
309
308
}
310
309
311
310
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" ) ;
315
312
}
316
313
317
314
/**
@@ -350,17 +347,16 @@ export class GroupBy {
350
347
let columns = Object . keys ( kwargs ) ;
351
348
let operations = columns . map ( ( x ) => { return kwargs [ x ] . toLocaleLowerCase ( ) ; } ) ;
352
349
353
- this . col ( columns ) ;
350
+ let col_gp = this . col ( columns ) ;
354
351
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 ) ;
357
354
358
355
return df ;
359
356
}
360
357
361
358
to_DataFrame ( key_col , col , data , ops ) {
362
359
363
- // console.log(data);
364
360
if ( key_col . length == 2 ) {
365
361
let df_data = [ ] ;
366
362
for ( let key_1 in data ) {
@@ -455,4 +451,61 @@ export class GroupBy {
455
451
}
456
452
}
457
453
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
+
458
511
}
0 commit comments