Skip to content

Commit 759b594

Browse files
committed
Merge branch 'danfo/typescript' of https://github.com/javascriptdata/danfojs into danfo/typescript
2 parents 868b065 + f635944 commit 759b594

File tree

10 files changed

+163
-48
lines changed

10 files changed

+163
-48
lines changed

src/danfojs-base/aggregators/groupby.ts

Lines changed: 99 additions & 14 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;
@@ -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]])

src/danfojs-base/core/daterange.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ class DateRange {
130130
return dateString
131131
}
132132

133+
/**
134+
* @param date Date
135+
* @param ftype string: frequency type, month, Year, day etc
136+
* @param number
137+
*/
133138
private freqType(date: Date, ftype: string): number{
134139
let rslt: number = 0;
135140
switch (ftype){
@@ -252,15 +257,15 @@ class DateRange {
252257
}
253258

254259
/**
255-
*
260+
* Generate sequence of Dates
256261
* @param start : signify the date to start with
257262
* @param end : signify the date to end with
258263
* @param period : the total number of date to generate
259264
* @param offset : set the date range offset
260265
* @param freq: set the date range frequency and offset
261266
* @return string[]
262267
*/
263-
export default function date_range(param: Params): string[] {
268+
export default function dateRange(param: Params): string[] {
264269
const dateRange = new DateRange(param)
265270
return dateRange.range()
266271
}

src/danfojs-base/core/frame.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,14 +1739,20 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
17391739
addColumn(
17401740
column: string,
17411741
values: Series | ArrayType1D,
1742-
options?: { inplace?: boolean, atIndex?: number }
1742+
options?: { inplace?: boolean, atIndex?: number | string }
17431743
): DataFrame
17441744
addColumn(
17451745
column: string,
17461746
values: Series | ArrayType1D,
1747-
options?: { inplace?: boolean, atIndex?: number }
1747+
options?: { inplace?: boolean, atIndex?: number | string }
17481748
): DataFrame | void {
1749-
const { inplace, atIndex } = { inplace: false, atIndex: this.columns.length, ...options };
1749+
let { inplace, atIndex } = { inplace: false, atIndex: this.columns.length, ...options };
1750+
if (typeof atIndex === "string" ) {
1751+
if (!(this.columns.includes(atIndex))){
1752+
throw new Error(`${atIndex} not a column`)
1753+
}
1754+
atIndex = this.columns.indexOf(atIndex)
1755+
}
17501756

17511757
if (!column) {
17521758
throw new Error("ParamError: column must be specified")
@@ -3132,6 +3138,12 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
31323138
/**
31333139
* Groupby
31343140
* @params col a list of column
3141+
* @returns Groupby
3142+
* @example
3143+
* let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
3144+
* let cols = [ "A", "B", "C" ];
3145+
* let df = new dfd.DataFrame(data, { columns: cols });
3146+
* let groupDf = df.groupby([ "A" ]);
31353147
*/
31363148
groupby(col: Array<string>): Groupby {
31373149
const columns = this.columns

src/danfojs-base/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import OneHotEncoder from "./transformers/encoders/one.hot.encoder";
2626
import getDummies from "./transformers/encoders/dummy.encoder"
2727
import concat from "./transformers/concat"
2828
import merge from "./transformers/merge"
29-
import date_range from "./core/daterange"
29+
import dateRange from "./core/daterange"
3030
import tensorflow from "./shared/tensorflowlib"
3131

3232
const __version = "1.0.0"
@@ -47,7 +47,7 @@ export {
4747
getDummies,
4848
concat,
4949
merge,
50-
date_range,
50+
dateRange,
5151
tensorflow,
5252
__version,
5353
}

src/danfojs-base/shared/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export interface DataFrameInterface extends NDframeInterface {
250250
values: Series | ArrayType1D,
251251
options?: {
252252
inplace?: boolean,
253-
atIndex?: number
253+
atIndex?: number | string
254254
}
255255
): DataFrame | void
256256
groupby(col: Array<string>): Groupby

src/danfojs-base/transformers/concat.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import Series from "../core/series"
1717
import DataFrame from "../core/frame"
1818
import { ArrayType1D, ArrayType2D } from "../shared/types"
1919

20+
/**
21+
*
22+
* @param dfList Array<DataFrame | Series>
23+
* @param axis number
24+
* @returns DataFrame
25+
*/
2026
function processColumn(dfList: Array<DataFrame | Series>, axis: number ): DataFrame {
2127
let allDf: any = {}
2228
let dublicateColumns: any = {}
@@ -58,7 +64,12 @@ function processColumn(dfList: Array<DataFrame | Series>, axis: number ): DataFr
5864
return new DataFrame(allDf)
5965
}
6066

61-
67+
/**
68+
* Concat data along rows
69+
* @param dfList Array<DataFrame | Series>
70+
* @param axis Array<DataFrame | Series>
71+
* @returns DataFrame
72+
*/
6273
function processRow(dfList: Array<DataFrame | Series>, axis: number ): DataFrame | Series {
6374
let allDf: any = {}
6475
let maxLen = 0
@@ -120,6 +131,8 @@ function processRow(dfList: Array<DataFrame | Series>, axis: number ): DataFrame
120131
* dfList: Array of DataFrame or Series
121132
* axis: axis of concatenation 1 or 0
122133
* @returns {DataFrame}
134+
* @example
135+
* concat({dfList: [df1, df2, df3], axis: 1})
123136
*/
124137
function concat({dfList, axis}: {
125138
dfList : Array<DataFrame | Series>,

src/danfojs-browser/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
concat,
2727
merge,
2828
toDateTime,
29-
date_range,
29+
dateRange,
3030
tensorflow,
3131
__version,
3232
} from "../../danfojs-base";
@@ -67,7 +67,7 @@ export {
6767
getDummies,
6868
concat,
6969
merge,
70-
date_range,
70+
dateRange,
7171
tensorflow,
7272
__version,
7373
}

0 commit comments

Comments
 (0)