Skip to content

Commit a978fc8

Browse files
committed
optimized drop method in frame
1 parent 99a2f4a commit a978fc8

3 files changed

Lines changed: 23 additions & 19 deletions

File tree

danfojs/src/core/frame.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,36 +79,37 @@ export class DataFrame extends Ndframe {
7979
throw Error("No column found. Axis of 1 must be accompanied by an array of column(s) names")
8080
}
8181
let self = this;
82+
let new_col_data = {}
83+
let new_dtype = []
84+
8285
const index = data.map((x) => {
8386
let col_idx = self.columns.indexOf(x)
8487
if (col_idx == -1) {
8588
throw new Error(`column "${x}" does not exist`)
8689
}
8790
return col_idx
8891
});
89-
const values = this.values
90-
let new_dtype = []
91-
let new_data = values.map(function (element) {
92-
let new_arr = utils.__remove_arr(element, index);
93-
new_dtype = utils.__remove_arr(self.dtypes, index);
94-
return new_arr;
95-
});
92+
93+
this.col_data.forEach((col, idx) => {
94+
if (!index.includes(idx)) {
95+
new_col_data[self.column_names[idx]] = col
96+
new_dtype.push(self.dtypes[idx])
97+
}
98+
})
9699

97100
if (!kwargs['inplace']) {
98-
let old_cols = this.columns
99-
let columns = utils.__remove_arr(this.columns, index);
100-
let df = new DataFrame(new_data, { columns: columns, index: self.index, dtypes: new_dtype })
101-
df.__set_col_property(df, df.col_data, columns, old_cols)
101+
let old_cols = self.columns
102+
let new_columns = Object.keys(new_col_data)
103+
let df = new DataFrame(new_col_data, { index: self.index, dtypes: new_dtype })
104+
df.__set_col_property(df, df.col_data, new_columns, old_cols)
102105
return df
103106

104107
} else {
105-
let new_cols = utils.__remove_arr(this.columns, index);
106-
let old_cols = this.columns
107-
this.columns = new_cols
108-
this.row_data_tensor = tf.tensor(new_data);
109-
this.data = new_data
110-
this.__set_col_types(new_dtype, false)
111-
this.__set_col_property(this, this.col_data, new_cols, old_cols)
108+
let old_cols = self.columns
109+
let new_columns = Object.keys(new_col_data)
110+
this.__update_frame_in_place(null, null, new_col_data, null,new_dtype)
111+
this.__set_col_property(self, self.col_data, new_columns, old_cols)
112+
112113
}
113114

114115
} else {

danfojs/tests/core/frame.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ describe("DataFrame", function () {
3333
df.drop({ columns: ["C"], axis: 1, inplace: true });
3434
let new_data = [[1, 2], [4, 5]]
3535
assert.deepEqual(df.values, new_data);
36+
assert.deepEqual(df.dtypes.length, 2);
37+
3638
})
3739

3840
it("check if data is updated after row is dropped", function () {
@@ -77,6 +79,7 @@ describe("DataFrame", function () {
7779
df.drop({ index: ["a", "b"], axis: 0, inplace: true });
7880
let new_data = [[20, 34, 5]]
7981
assert.deepEqual(df.values, new_data);
82+
8083
})
8184
})
8285

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"table": "^5.4.6"
2424
},
2525
"scripts": {
26-
"test": "nyc mocha --require @babel/register danfojs/tests/*",
26+
"test": "nyc mocha --require @babel/register danfojs/tests/core/frame",
2727
"dev": "npm run lint && babel ./danfojs/src -d dist --no-comments",
2828
"build": "babel ./danfojs/src -d ./dist --no-comments",
2929
"lint": "eslint ./danfojs/src",

0 commit comments

Comments
 (0)