Skip to content

Commit a126baa

Browse files
committed
update addColumn to include atIndex
1 parent 96b8c58 commit a126baa

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

src/danfojs-base/core/frame.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,14 +1322,15 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
13221322
addColumn(
13231323
column: string,
13241324
values: Series | ArrayType1D,
1325-
options?: { inplace?: boolean }
1325+
options?: { inplace?: boolean, atIndex?: number }
13261326
): DataFrame
13271327
addColumn(
13281328
column: string,
13291329
values: Series | ArrayType1D,
1330-
options?: { inplace?: boolean }
1330+
options?: { inplace?: boolean, atIndex?: number }
13311331
): DataFrame | void {
13321332
const { inplace } = { inplace: false, ...options };
1333+
const atIndex = typeof options?.atIndex !== "undefined" ? options.atIndex : this.columns.length
13331334

13341335
if (!column) {
13351336
throw new Error("ParamError: column must be specified")
@@ -1360,19 +1361,24 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
13601361
const oldValues = this.$data
13611362
for (let i = 0; i < oldValues.length; i++) {
13621363
const innerArr = [...oldValues[i]] as ArrayType1D
1363-
innerArr.push(colunmValuesToAdd[i])
1364+
innerArr.splice(atIndex, 0, colunmValuesToAdd[i])
13641365
newData.push(innerArr)
13651366
}
13661367

13671368
if (inplace) {
13681369
this.$setValues(newData, true, false)
1369-
this.$setColumnNames([...this.columns, column]);
1370+
let columns = [...this.columns]
1371+
columns.splice(atIndex, 0, column)
1372+
this.$setColumnNames(columns)
13701373
this.$setInternalColumnDataProperty(column);
13711374

13721375
} else {
1376+
let columns = [...this.columns]
1377+
columns.splice(atIndex, 0, column)
1378+
13731379
const df = new DataFrame(newData, {
13741380
index: [...this.index],
1375-
columns: [...this.columns, column],
1381+
columns: columns,
13761382
dtypes: [...this.dtypes, utils.inferDtype(colunmValuesToAdd)[0]],
13771383
config: { ...this.$config }
13781384
})

0 commit comments

Comments
 (0)