Skip to content

Commit a8ae166

Browse files
committed
add docstring
1 parent 4b8c1fd commit a8ae166

1 file changed

Lines changed: 96 additions & 12 deletions

File tree

src/danfojs-base/aggregators/groupby.ts

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import concat from "../transformers/concat"
1919
import 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;
@@ -518,27 +582,47 @@ export default class Groupby {
518582
return concat({dfList: data, axis:0}) as DataFrame
519583
}
520584

585+
/**
586+
* obtain the total number of groups
587+
* @returns number
588+
*/
521589
get ngroups(): number{
522590
let keys = Object.keys(this.colDict)
523591
return keys.length
524592
}
525593

594+
/**
595+
* obtaind the internal group data
596+
* @returns {[keys: string]: {}}
597+
*/
526598
get groups(): {[keys: string]: {}}{
527599
return this.colDict
528600
}
529601

602+
/**
603+
* Obtain the first row of each group
604+
* @returns DataFrame
605+
*/
530606
first(): DataFrame{
531607
return this.apply((x)=>{
532608
return x.head(1)
533609
})
534610
}
535611

612+
/**
613+
* Obtain the last row of each group
614+
* @returns DataFrame
615+
*/
536616
last(): DataFrame {
537617
return this.apply((x)=>{
538618
return x.tail(1)
539619
})
540620
}
541621

622+
/**
623+
* Obtains the dataframe se of each groups
624+
* @returns DataFrame
625+
*/
542626
size(): DataFrame {
543627
return this.apply((x)=>{
544628
return new Series([x.shape[0]])

0 commit comments

Comments
 (0)