Skip to content

Commit 11cfe87

Browse files
committed
update addColumn to accept Series and update dtype
1 parent bed5992 commit 11cfe87

2 files changed

Lines changed: 86 additions & 35 deletions

File tree

danfojs/src/core/frame.js

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class DataFrame extends Ndframe {
3333
let col_names = this.column_names
3434

3535
col_vals.forEach((col, i) => {
36-
this[col_names[i]] = new Series(col, { columns: col_names[i], index: this.index })
36+
this[col_names[i]] = null
3737
Object.defineProperty(this, col_names[i], {
3838
get() {
3939
return new Series(this.col_data[i], { columns: col_names[i], index: this.index })
@@ -1029,34 +1029,33 @@ export class DataFrame extends Ndframe {
10291029
*/
10301030
addColumn(kwargs) {
10311031

1032-
let data_length = this.shape[0]
1033-
10341032
utils.__in_object(kwargs, "column", "column name not specified");
10351033
utils.__in_object(kwargs, "value", "column value not specified");
10361034

1037-
let value = kwargs["value"]
10381035
let column_name = kwargs["column"]
1036+
let data_length = this.shape[0]
1037+
let value;
1038+
1039+
if (kwargs['value'] instanceof Series) {
1040+
value = kwargs['value'].values
1041+
} else {
1042+
value = kwargs["value"]
1043+
}
10391044

10401045
if (value.length != data_length) {
10411046
throw new Error(`Array length ${value.length} not equal to ${data_length}`);
10421047
}
10431048

1044-
10451049
if (this.columns.includes(column_name)) {
1046-
10471050
let col_idx = this.columns.indexOf(column_name);
1048-
10491051
let new_data = []
1052+
10501053
this.values.map((val, index) => {
10511054
let new_val = val.slice();
10521055
new_val[col_idx] = value[index]
10531056
new_data.push(new_val);
10541057
})
1055-
this.data = new_data;
1056-
// console.log(this.data)
1057-
this.col_data[col_idx] = value
1058-
// this.col_data[col_idx] = utils.__get_t(value)[0]
1059-
this.data_tensor = tf.tensor(new_data)
1058+
this.__update_frame_in_place(new_data, null, null, null, null)
10601059

10611060

10621061
} else {
@@ -1070,18 +1069,13 @@ export class DataFrame extends Ndframe {
10701069
});
10711070

10721071
//add new dtype
1073-
let old_type_list = [...this.dtypes]
1074-
old_type_list.push(utils.__get_t(value)[0])
1075-
this.col_types = old_type_list
1076-
this.data = new_data;
1077-
this.col_data = utils.__get_col_values(new_data)
1078-
this.data_tensor = tf.tensor(new_data)
1079-
let old_col_names = this.columns
1080-
old_col_names.push(column_name)
1081-
let new_cols = old_col_names
1082-
this.columns = new_cols
1083-
this[column_name] = new Series(value)
1084-
// this.__set_col_property(this, this.col_data, new_cols, old_col_names,true)
1072+
let new_dtypes = [...this.dtypes]
1073+
new_dtypes.push(utils.__get_t(value)[0])
1074+
1075+
let new_col_names = [...this.columns]
1076+
new_col_names.push(column_name)
1077+
1078+
this.__update_frame_in_place(new_data, new_col_names, null, null, new_dtypes)
10851079
Object.defineProperty(this, column_name, {
10861080
get() {
10871081
return new Series(value, { columns: column_name, index: this.index })
@@ -1191,11 +1185,15 @@ export class DataFrame extends Ndframe {
11911185
*/
11921186
fillna(kwargs = {}) {
11931187

1194-
let params_needed = ["columns", "values"]
1188+
let params_needed = ["columns", "values", "inplace"]
11951189
if (!utils.__right_params_are_passed(kwargs, params_needed)) {
11961190
throw Error(`Params Error: A specified parameter is not supported. Your params must be any of the following [${params_needed}], got ${Object.keys(kwargs)}`)
11971191
}
11981192

1193+
if (!utils.__key_in_object(kwargs, "inplace")) {
1194+
kwargs['inplace'] = false
1195+
}
1196+
11991197
if (utils.__key_in_object(kwargs, "columns")) {
12001198
//check if the column(s) exists
12011199
kwargs['columns'].map(col => {
@@ -1232,7 +1230,12 @@ export class DataFrame extends Ndframe {
12321230
final_data[this.column_names[i]] = col
12331231
})
12341232

1235-
return new DataFrame(final_data, { index: this.index })
1233+
if (kwargs['inplace']) {
1234+
this.__update_frame_in_place(null, null, final_data, null, null)
1235+
} else {
1236+
return new DataFrame(final_data, { index: this.index })
1237+
1238+
}
12361239

12371240
} else {
12381241
//fill all columns using same value
@@ -1873,7 +1876,7 @@ export class DataFrame extends Ndframe {
18731876
* @returns {2D tensor}
18741877
*/
18751878
get tensor() {
1876-
return this.row_data_tensor
1879+
return tf.tensor(this.values)
18771880
}
18781881

18791882

@@ -2101,15 +2104,31 @@ export class DataFrame extends Ndframe {
21012104
}
21022105

21032106
//update a DataFrame in place
2104-
__update_frame_in_place(values, column_names, col_data, index, dtypes) {
2105-
if (values != undefined) {
2106-
this.data = values
2107-
if (col_data == undefined || col_data == null) {
2108-
this.col_data = utils.__get_col_values(values) //update column values
2109-
} else {
2110-
this.col_data = col_data
2107+
__update_frame_in_place(row_data, column_names, col_obj, index, dtypes) {
2108+
if (row_data != undefined) {
2109+
this.data = row_data
2110+
} else {
2111+
//check column is available and create row from column
2112+
if (col_obj != undefined) {
2113+
let _res = utils.__get_row_values(col_obj)
2114+
this.data = _res[0]
2115+
this.columns = _res[1]
2116+
column_names = _res[1]
21112117
}
21122118
}
2119+
2120+
if (col_obj != undefined) {
2121+
this.col_data = Object.values(col_obj)
2122+
this.columns = Object.keys(col_obj)
2123+
column_names = Object.keys(col_obj)
2124+
} else {
2125+
//check if row data is available and create column data from rows
2126+
if (row_data != undefined) {
2127+
this.col_data = utils.__get_col_values(row_data) //get column data from row
2128+
}
2129+
}
2130+
2131+
21132132
if (column_names != undefined) {
21142133
this.columns = column_names
21152134
}

danfojs/tests/core/frame.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,30 @@ describe("DataFrame", function () {
999999
assert.throws(function () { df.addColumn({ "column": "D", "value": new_col }); }, Error, "Array length 3 not equal to 4");
10001000
});
10011001

1002+
it("Check that dtype is updated after a new column is added ", function () {
1003+
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
1004+
let cols = ["A", "B", "C"]
1005+
let df = new DataFrame(data, { columns: cols })
1006+
let new_col = ["n", "b", "c", "f"]
1007+
1008+
df.addColumn({ "column": "D", "value": new_col });
1009+
let dtype = ["int32", "int32", "int32", "string"]
1010+
1011+
assert.deepEqual(df.dtypes, dtype);
1012+
});
1013+
1014+
it("add series as value to a new column ", function () {
1015+
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
1016+
let cols = ["A", "B", "C"]
1017+
let df = new DataFrame(data, { columns: cols })
1018+
let sf = new Series(["n", "b", "c", "f"])
1019+
1020+
df.addColumn({ "column": "D", "value": sf });
1021+
let dtype = ["int32", "int32", "int32", "string"]
1022+
1023+
assert.deepEqual(df.dtypes, dtype);
1024+
});
1025+
10021026
});
10031027

10041028
// describe("groupby", function () {
@@ -1399,14 +1423,22 @@ describe("DataFrame", function () {
13991423
assert.deepEqual(df_filled.values, new_vals);
14001424
});
14011425

1402-
it("Fills a list of columns with specified values inplace", function () {
1426+
it("Fills a list of columns with specified values", function () {
14031427
let data = [[1, undefined, 3], [4, undefined, 6], [NaN, "boy", 40], [NaN, "girl", 78]]
14041428
let cols = ["A", "B", "C"]
14051429
let df = new DataFrame(data, { columns: cols })
14061430
let new_vals = [[1, "girl", 3], [4, "girl", 6], [200, "boy", 40], [200, "girl", 78]]
14071431
let df_filled = df.fillna({ columns: ["A", "B"], values: [200, "girl"] })
14081432
assert.deepEqual(df_filled.values, new_vals);
14091433
});
1434+
it("Fills a list of columns with specified values inplace", function () {
1435+
let data = [[1, undefined, 3], [4, undefined, 6], [NaN, "boy", 40], [NaN, "girl", 78]]
1436+
let cols = ["A", "B", "C"]
1437+
let df = new DataFrame(data, { columns: cols })
1438+
let new_vals = [[1, "girl", 3], [4, "girl", 6], [200, "boy", 40], [200, "girl", 78]]
1439+
df.fillna({ columns: ["A", "B"], values: [200, "girl"], inplace: true })
1440+
assert.deepEqual(df.values, new_vals);
1441+
});
14101442
})
14111443

14121444

0 commit comments

Comments
 (0)