Skip to content

Commit e983b14

Browse files
committed
add column as atIndex
1 parent 98383d0 commit e983b14

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

src/danfojs-base/core/frame.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,15 +1615,29 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
16151615
addColumn(
16161616
column: string,
16171617
values: Series | ArrayType1D,
1618-
options?: { inplace?: boolean, atIndex?: number }
1618+
options?: { inplace?: boolean, atIndex?: number | string }
16191619
): DataFrame
16201620
addColumn(
16211621
column: string,
16221622
values: Series | ArrayType1D,
1623-
options?: { inplace?: boolean, atIndex?: number }
1623+
options?: { inplace?: boolean, atIndex?: number | string }
16241624
): DataFrame | void {
16251625
const { inplace } = { inplace: false, ...options };
1626-
const atIndex = typeof options?.atIndex !== "undefined" ? options.atIndex : this.columns.length
1626+
let atIndex;
1627+
if(typeof options?.atIndex !== "undefined" ) {
1628+
if (typeof options?.atIndex === "string" ) {
1629+
if (!(this.columns.includes(options?.atIndex))){
1630+
throw new Error(`${options?.atIndex} not a column`)
1631+
}
1632+
atIndex = this.columns.indexOf(options?.atIndex)
1633+
}
1634+
else {
1635+
atIndex = options?.atIndex
1636+
}
1637+
}
1638+
else {
1639+
atIndex = this.columns.length
1640+
}
16271641

16281642
if (!column) {
16291643
throw new Error("ParamError: column must be specified")
@@ -2726,6 +2740,12 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
27262740
/**
27272741
* Groupby
27282742
* @params col a list of column
2743+
* @returns Groupby
2744+
* @example
2745+
* let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
2746+
* let cols = [ "A", "B", "C" ];
2747+
* let df = new dfd.DataFrame(data, { columns: cols });
2748+
* let groupDf = df.groupby([ "A" ]);
27292749
*/
27302750
groupby(col: Array<string>): Groupby {
27312751
const columns = this.columns

0 commit comments

Comments
 (0)