@@ -19,6 +19,7 @@ import concat from "../transformers/concat"
1919import Series from "../core/series" ;
2020
2121
22+
2223/**
2324 * The class performs all groupby operation on a dataframe
2425 * involving all aggregate funciton
@@ -422,61 +423,124 @@ export default class Groupby {
422423 return df
423424 }
424425
425- count ( ) {
426+ /**
427+ * Obtain the count for each group
428+ * @returns DataFrame
429+ *
430+ */
431+ count ( ) : DataFrame {
426432 return this . operations ( "count" )
427433 }
428434
429- sum ( ) {
435+ /**
436+ * Obtain the sum of columns for each group
437+ * @returns DataFrame
438+ *
439+ */
440+ sum ( ) : DataFrame {
430441 return this . operations ( "sum" )
431442 }
432443
433- var ( ) {
444+ /**
445+ * Obtain the variance of columns for each group
446+ * @returns DataFrame
447+ */
448+ var ( ) : DataFrame {
434449 return this . operations ( "var" )
435450 }
436451
437- mean ( ) {
452+ /**
453+ * Obtain the mean of columns for each group
454+ * @returns DataFrame
455+ */
456+ mean ( ) : DataFrame {
438457 return this . operations ( "mean" )
439458 }
440459
441- cumsum ( ) {
460+ /**
461+ * Obtain the cumsum of columns for each group
462+ * @returns DataFrame
463+ *
464+ */
465+ cumsum ( ) : DataFrame {
442466 return this . operations ( "cumsum" )
443467 }
444468
445- cummax ( ) {
469+ /**
470+ * Obtain the cummax of columns for each group
471+ * @returns DataFrame
472+ */
473+ cummax ( ) : DataFrame {
446474 return this . operations ( "cummax" )
447475 }
448476
449- cumprod ( ) {
477+ /**
478+ * Obtain the cumprod of columns for each group
479+ * @returns DataFrame
480+ */
481+ cumprod ( ) : DataFrame {
450482 return this . operations ( "cumprod" )
451483 }
452484
453- cummin ( ) {
485+ /**
486+ * Obtain the cummin of columns for each group
487+ * @returns DataFrame
488+ */
489+ cummin ( ) : DataFrame {
454490 return this . operations ( "cummin" )
455491 }
456492
457- max ( ) {
493+ /**
494+ * Obtain the max value of columns for each group
495+ * @returns DataFrame
496+ *
497+ */
498+ max ( ) : DataFrame {
458499 return this . operations ( "max" )
459500 }
460501
461- min ( ) {
502+ /**
503+ * Obtain the min of columns for each group
504+ * @returns DataFrame
505+ */
506+ min ( ) : DataFrame {
462507 return this . operations ( "min" )
463508 }
464509
510+ /**
511+ * Obtain a specific group
512+ * @param keys Array<string | number>
513+ * @returns DataFrame
514+ */
465515 getGroup ( keys : Array < string | number > ) : DataFrame {
466516 let dictKey = keys . join ( "-" )
467517 let colDict : { [ key : string ] : { } } = { }
468518 colDict [ dictKey ] = { ...this . colDict [ dictKey ] }
469519 return this . toDataFrame ( colDict )
470520 }
471521
472- agg ( ops : { [ key : string ] : Array < string > | string } ) {
522+ /**
523+ * Perform aggregation on all groups
524+ * @param ops
525+ * @returns DataFrame
526+ */
527+ agg ( ops : { [ key : string ] : Array < string > | string } ) : DataFrame {
473528 let columns = Object . keys ( ops ) ;
474529 let col_gp = this . col ( columns ) ;
475530 let data = col_gp . arithemetic ( ops ) ;
476531 let df = col_gp . toDataFrame ( data ) ;
477532 return df ;
478533 }
479534
535+ /**
536+ * Apply custom aggregator function
537+ * to each group
538+ * @param callable
539+ * @returns DataFrame
540+ * @example
541+ * let grp = df.groupby(['A'])
542+ * grp.apply((x) => x.count())
543+ */
480544 apply ( callable : ( x : DataFrame ) => DataFrame | Series ) : DataFrame {
481545 let colDict : { [ key : string ] : DataFrame | Series } = { }
482546 for ( const key of this . colKeyDict ( this . colDict ) ) {
@@ -486,7 +550,7 @@ export default class Groupby {
486550 return this . concatGroups ( colDict )
487551 }
488552
489- concatGroups ( colDict : { [ key : string ] : DataFrame | Series } ) : DataFrame {
553+ private concatGroups ( colDict : { [ key : string ] : DataFrame | Series } ) : DataFrame {
490554 let data : Array < DataFrame | Series > = [ ]
491555 for ( const [ key , values ] of Object . entries ( colDict ) ) {
492556 let copyDf : DataFrame ;
@@ -505,11 +569,12 @@ export default class Groupby {
505569 let keyName = this . keyCol [ key1 ] as string
506570 let keyValue = this . keyToValue [ key ] [ key1 ]
507571 let dfValue = Array ( len ) . fill ( keyValue )
572+ let atIndex : number = parseInt ( key1 )
508573 if ( this . groupColNames ) {
509- copyDf . addColumn ( keyName , dfValue , { inplace : true , atIndex : key1 as number } )
574+ copyDf . addColumn ( keyName , dfValue , { inplace : true , atIndex : atIndex } )
510575 }
511576 else {
512- copyDf . addColumn ( `${ keyName } _Group` , dfValue , { inplace : true , atIndex : key1 as number } )
577+ copyDf . addColumn ( `${ keyName } _Group` , dfValue , { inplace : true , atIndex : atIndex } )
513578 }
514579
515580 }
@@ -518,27 +583,47 @@ export default class Groupby {
518583 return concat ( { dfList : data , axis :0 } ) as DataFrame
519584 }
520585
586+ /**
587+ * obtain the total number of groups
588+ * @returns number
589+ */
521590 get ngroups ( ) : number {
522591 let keys = Object . keys ( this . colDict )
523592 return keys . length
524593 }
525594
595+ /**
596+ * obtaind the internal group data
597+ * @returns {[keys: string]: {} }
598+ */
526599 get groups ( ) : { [ keys : string ] : { } } {
527600 return this . colDict
528601 }
529602
603+ /**
604+ * Obtain the first row of each group
605+ * @returns DataFrame
606+ */
530607 first ( ) : DataFrame {
531608 return this . apply ( ( x ) => {
532609 return x . head ( 1 )
533610 } )
534611 }
535612
613+ /**
614+ * Obtain the last row of each group
615+ * @returns DataFrame
616+ */
536617 last ( ) : DataFrame {
537618 return this . apply ( ( x ) => {
538619 return x . tail ( 1 )
539620 } )
540621 }
541622
623+ /**
624+ * Obtains the dataframe se of each groups
625+ * @returns DataFrame
626+ */
542627 size ( ) : DataFrame {
543628 return this . apply ( ( x ) => {
544629 return new Series ( [ x . shape [ 0 ] ] )
0 commit comments