Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/danfojs-base/aggregators/groupby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export default class Groupby {
private arithemetic(operation: {[key: string] : Array<string> | string} | string): { [key: string ]: {} } {

const opsName = [ "mean", "sum", "count", "mode", "std", "var", "cumsum", "cumprod",
"cummax", "cummin", "median" , "min", "max"];
"cummax", "cummin", "median" , "min", "max", "countdistinct"];
if (typeof operation === "string" ) {
if (!opsName.includes(operation)) {
throw new Error(`group operation: ${operation} is not valid`)
Expand Down Expand Up @@ -377,6 +377,9 @@ export default class Groupby {
return sum
}, 1)
break;
case "countdistinct":
data.push(new Set(colVal).size);
break;
}
return data
}
Expand Down Expand Up @@ -516,6 +519,14 @@ export default class Groupby {
return this.operations("min")
}

/**
* Obtain the distinct number of columns for each group
* @returns DataFrame
*/
countDistinct(): DataFrame{
return this.operations("countdistinct")
}

/**
* Obtain a specific group
* @param keys Array<string | number>
Expand Down
5 changes: 5 additions & 0 deletions src/danfojs-base/core/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,11 @@ export default class Series extends NDframe implements SeriesInterface {
return this.cumOps("min", ops);
}

countDistinct(options?: { inplace?: boolean; }): Series | void {
const ops = { inplace: false, ...options }
return this.cumOps("countdistinct", ops);
}


/**
* Returns cumulative maximum over a Series
Expand Down
2 changes: 2 additions & 0 deletions src/danfojs-base/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export interface SeriesInterface extends NDframeInterface {
cumMin(options?: { inplace?: boolean }): Series | void
cumMax(options?: { inplace?: boolean }): Series | void
cumProd(options?: { inplace?: boolean }): Series | void
countDistinct(options?: { inplace?: boolean }): Series | void
lt(other: Series | number | Array<number> | boolean[]): Series
gt(other: Series | number | Array<number> | boolean[]): Series
le(other: Series | number | Array<number> | boolean[]): Series
Expand Down Expand Up @@ -233,6 +234,7 @@ export interface DataFrameInterface extends NDframeInterface {
cumMin(options?: { axis?: 0 | 1 }): DataFrame | void
cumMax(options?: { axis?: 0 | 1 }): DataFrame | void
cumProd(options?: { axis?: 0 | 1 }): DataFrame | void
countDistinct(options?: { axis?: 0 | 1 }): DataFrame | void
copy(): DataFrame
resetIndex(options: { inplace?: boolean }): DataFrame | void
setIndex(
Expand Down