Skip to content

Commit bed5992

Browse files
committed
add inplace option for query
1 parent 5716de9 commit bed5992

3 files changed

Lines changed: 119 additions & 56 deletions

File tree

danfojs/src/core/frame.js

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -961,24 +961,25 @@ export class DataFrame extends Ndframe {
961961
"=="
962962
]
963963

964-
if (Object.prototype.hasOwnProperty.call(kwargs, "column")) {
964+
if (!utils.__key_in_object(kwargs, "inplace")) {
965+
kwargs['inplace'] = false
966+
}
965967

968+
let column_index, operator, value;
966969

970+
if (utils.__key_in_object(kwargs, "column")) {
967971
if (this.columns.includes(kwargs["column"])) {
968-
969-
var column_index = this.columns.indexOf(kwargs["column"]);
972+
column_index = this.columns.indexOf(kwargs["column"]);
970973
} else {
971974
throw new Error(`column ${kwargs["column"]} does not exist`);
972975
}
973976
} else {
974977
throw new Error("specify the column");
975978
}
976979

977-
if (Object.prototype.hasOwnProperty.call(kwargs, "is")) {
978-
980+
if (utils.__key_in_object(kwargs, "is")) {
979981
if (operators.includes(kwargs["is"])) {
980-
981-
var operator = kwargs["is"];
982+
operator = kwargs["is"];
982983
}
983984
else {
984985
throw new Error(` ${kwargs["is"]} is not a supported logical operator`);
@@ -987,34 +988,37 @@ export class DataFrame extends Ndframe {
987988
throw new Error("specify an operator in param [is]");
988989
}
989990

990-
if (Object.prototype.hasOwnProperty.call(kwargs, "to")) {
991-
var value = kwargs["to"]
991+
if (utils.__key_in_object(kwargs, "to")) {
992+
value = kwargs["to"]
992993

993994
} else {
994995
throw new Error("specify a value in param [to]");
995996
}
996997

997998
let data = this.values
998-
999+
let index = this.index
9991000
let new_data = []
1001+
let new_index = []
10001002

10011003
for (var i = 0; i < data.length; i++) {
10021004
let data_value = data[i]
1003-
10041005
let elem = data_value[column_index]
1005-
10061006
//use eval function for easy operation
10071007
//eval() takes in a string expression e.g eval('2>5')
10081008
if (eval(`${elem}${operator}${value}`)) {
10091009
new_data.push(data_value);
1010-
}
1010+
new_index.push(index[i])
10111011

1012+
}
10121013

10131014
}
1014-
let columns = this.columns
1015-
let new_df = new DataFrame(new_data, { "columns": columns })
10161015

1017-
return new_df;
1016+
if (kwargs['inplace']) {
1017+
this.__update_frame_in_place(new_data, this.columns, null, new_index, null)
1018+
} else {
1019+
let new_df = new DataFrame(new_data, { "columns": this.columns, index: new_index })
1020+
return new_df;
1021+
}
10181022
}
10191023

10201024

@@ -1081,6 +1085,8 @@ export class DataFrame extends Ndframe {
10811085
Object.defineProperty(this, column_name, {
10821086
get() {
10831087
return new Series(value, { columns: column_name, index: this.index })
1088+
}, set(value) {
1089+
this.addColumn({ column: column_name, value: value });
10841090
}
10851091
})
10861092
}
@@ -2021,9 +2027,6 @@ export class DataFrame extends Ndframe {
20212027
if (!utils.__key_in_object(kwargs, "mapper")) {
20222028
throw Error("Please specify a mapper object")
20232029
}
2024-
this.print()
2025-
console.log(kwargs['axis']);
2026-
console.log(kwargs['inplace']);
20272030
if (kwargs['axis'] == 1) {
20282031
//columns
20292032
let old_col_names = Object.keys(kwargs['mapper'])
@@ -2039,19 +2042,13 @@ export class DataFrame extends Ndframe {
20392042
col_names[idx] = new_col_names[i]
20402043

20412044
})
2042-
console.log("Before rename");
2043-
this.print()
20442045
if (kwargs['inplace']) {
20452046
this.columns = col_names
20462047
this.__set_col_property(this, this.col_data, col_names, old_col_names)
2047-
console.log("after rename inplace");
2048-
this.print()
20492048
} else {
20502049
let df = this.copy()
20512050
df.columns = col_names
20522051
df.__set_col_property(df, df.col_data, col_names, old_col_names)
2053-
console.log("after rename not inplace");
2054-
df.print()
20552052
return df
20562053
}
20572054
} else {
@@ -2095,10 +2092,33 @@ export class DataFrame extends Ndframe {
20952092
Object.defineProperty(self, col_names[i], {
20962093
get() {
20972094
return new Series(col, { columns: col_names[i], index: self.index })
2095+
}, set(value) {
2096+
this.addColumn({ column: col_names[i], value: value });
20982097
}
20992098
})
21002099
});
21012100

21022101
}
2102+
2103+
//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
2111+
}
2112+
}
2113+
if (column_names != undefined) {
2114+
this.columns = column_names
2115+
}
2116+
if (index != undefined) {
2117+
this.index_arr = index
2118+
}
2119+
if (dtypes != undefined) {
2120+
this.col_types = dtypes
2121+
}
2122+
}
21032123
}
21042124

danfojs/tests/core/frame.js

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,9 @@ describe("DataFrame", function () {
668668
describe("describe", function () {
669669
it("Returns descriptive statistics of columns in a DataFrame created from an array", function () {
670670
let data = [[0, 2, 4, "a"],
671-
[360, 180, 360, "b"],
672-
[2, 4, 6, "c"]]
673-
671+
[360, 180, 360, "b"],
672+
[2, 4, 6, "c"]]
673+
674674
let df = new DataFrame(data)
675675
let res = [[3, 3, 3], [120.666664, 62, 123.333336],
676676
[207.271159, 102.19589, 204.961785],
@@ -919,6 +919,39 @@ describe("DataFrame", function () {
919919

920920
assert.throws(function () { df.query({ "column": "D", "is": ">=", "to": 5 }) }, Error, "column D does not exist");
921921
});
922+
it("Confirms that query index are updated", function () {
923+
924+
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
925+
let cols = ["A", "B", "C"]
926+
let df = new DataFrame(data, { columns: cols })
927+
let df_query = df.query({ "column": "B", "is": ">=", "to": 5 })
928+
assert.deepEqual(df_query.index, [1, 2, 3])
929+
});
930+
it("Confirms that columns data are updated inplace", function () {
931+
932+
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
933+
let cols = ["A", "B", "C"]
934+
let df = new DataFrame(data, { columns: cols })
935+
df.query({ "column": "B", "is": ">=", "to": 5, inplace: true })
936+
assert.deepEqual(df.col_data, [[4, 20, 39], [5, 30, 89], [6, 40, 78]])
937+
});
938+
it("Confirms that query happens inplace", function () {
939+
940+
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
941+
let cols = ["A", "B", "C"]
942+
let df = new DataFrame(data, { columns: cols })
943+
df.query({ "column": "B", "is": ">=", "to": 5, inplace: true })
944+
let query_data = [[4, 5, 6], [20, 30, 40], [39, 89, 78]]
945+
assert.deepEqual(df.values, query_data)
946+
});
947+
it("Confirms that query happens inplace and index are updated", function () {
948+
949+
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
950+
let cols = ["A", "B", "C"]
951+
let df = new DataFrame(data, { columns: cols })
952+
df.query({ "column": "B", "is": ">=", "to": 5, inplace: true })
953+
assert.deepEqual(df.index, [1, 2, 3])
954+
});
922955

923956
});
924957

@@ -1346,9 +1379,9 @@ describe("DataFrame", function () {
13461379

13471380
it("Fills only a specified column", function () {
13481381
let data = [[1, 2, 3],
1349-
[4, 5, 6],
1350-
[20, NaN, 40],
1351-
[39, NaN, 78]]
1382+
[4, 5, 6],
1383+
[20, NaN, 40],
1384+
[39, NaN, 78]]
13521385
let cols = ["A", "B", "C"]
13531386
let df = new DataFrame(data, { columns: cols })
13541387
let new_vals = [[1, 2, 3], [4, 5, 6], [20, 2, 40], [39, 2, 78]]
@@ -1421,8 +1454,8 @@ describe("DataFrame", function () {
14211454

14221455
it("Returns string and float columns in a DataFrame", function () {
14231456
let data = [[30, 1, 2, "boy"],
1424-
[3.2, 4, 30, "girl"],
1425-
[5.09, 6, 7, "cat"]]
1457+
[3.2, 4, 30, "girl"],
1458+
[5.09, 6, 7, "cat"]]
14261459
let column = ["A", "B", "C", "D"]
14271460
let df = new DataFrame(data, { columns: column })
14281461
let df_sub = df.select_dtypes(['string', 'float32'])
@@ -1431,8 +1464,8 @@ describe("DataFrame", function () {
14311464

14321465
it("Returns int and float columns in a DataFrame", function () {
14331466
let data = [[30, 1, 2, "boy"],
1434-
[3.2, 4, 30, "girl"],
1435-
[5.09, 6, 7, "cat"]]
1467+
[3.2, 4, 30, "girl"],
1468+
[5.09, 6, 7, "cat"]]
14361469
let column = ["A", "B", "C", "D"]
14371470
let df = new DataFrame(data, { columns: column })
14381471
let df_sub = df.select_dtypes(['int32', 'float32'])
@@ -1935,10 +1968,12 @@ describe("DataFrame", function () {
19351968

19361969
describe("rename", function () {
19371970
it("Rename columns along axis 1", function () {
1938-
let data = { "A": [-20, 30, 47.3, -20],
1939-
"B": [34, -4, 5, 6] ,
1940-
"C": [20, 20, 30, 30] ,
1941-
"D": ["a", "b", "c", "c"] }
1971+
let data = {
1972+
"A": [-20, 30, 47.3, -20],
1973+
"B": [34, -4, 5, 6],
1974+
"C": [20, 20, 30, 30],
1975+
"D": ["a", "b", "c", "c"]
1976+
}
19421977

19431978
let ndframe = new DataFrame(data)
19441979
let df = ndframe.rename({ mapper: { "A": "a1", "B": "b1" } })
@@ -1947,10 +1982,12 @@ describe("DataFrame", function () {
19471982

19481983
})
19491984
it("confirms original column name is not modified along axis 1", function () {
1950-
let data = { "A": [-20, 30, 47.3, -20],
1951-
"B": [34, -4, 5, 6] ,
1952-
"C": [20, 20, 30, 30] ,
1953-
"D": ["a", "b", "c", "c"] }
1985+
let data = {
1986+
"A": [-20, 30, 47.3, -20],
1987+
"B": [34, -4, 5, 6],
1988+
"C": [20, 20, 30, 30],
1989+
"D": ["a", "b", "c", "c"]
1990+
}
19541991

19551992
let ndframe = new DataFrame(data)
19561993
let df = ndframe.rename({ mapper: { "A": "a1", "B": "b1" } })
@@ -1959,10 +1996,12 @@ describe("DataFrame", function () {
19591996

19601997
})
19611998
it("Rename columns along axis 1 inplace", function () {
1962-
let data = { "A": [-20, 30, 47.3, -20] ,
1963-
"B": [34, -4, 5, 6] ,
1964-
"C": [20, 20, 30, 30] ,
1965-
"D": ["a", "b", "c", "c"] }
1999+
let data = {
2000+
"A": [-20, 30, 47.3, -20],
2001+
"B": [34, -4, 5, 6],
2002+
"C": [20, 20, 30, 30],
2003+
"D": ["a", "b", "c", "c"]
2004+
}
19662005

19672006
let df = new DataFrame(data)
19682007
df.rename({ mapper: { "A": "a1", "B": "b1" }, inplace: true })
@@ -1971,10 +2010,12 @@ describe("DataFrame", function () {
19712010

19722011
})
19732012
it("Rename string index along axis 0", function () {
1974-
let data = { "A": [-20, 30, 47.3, -20] ,
1975-
"B": [34, -4, 5, 6] ,
1976-
"C": [20, 20, 30, 30] ,
1977-
"D": ["a", "b", "c", "c"] }
2013+
let data = {
2014+
"A": [-20, 30, 47.3, -20],
2015+
"B": [34, -4, 5, 6],
2016+
"C": [20, 20, 30, 30],
2017+
"D": ["a", "b", "c", "c"]
2018+
}
19782019

19792020
let ndframe = new DataFrame(data, { index: ["a", "b", "c", "d"] })
19802021
let df = ndframe.rename({ mapper: { "a": 0, "b": 1 }, axis: 0 })
@@ -1983,10 +2024,12 @@ describe("DataFrame", function () {
19832024

19842025
})
19852026
it("Rename string index along axis 0 inplace", function () {
1986-
let data = { "A": [-20, 30, 47.3, -20] ,
1987-
"B": [34, -4, 5, 6] ,
1988-
"C": [20, 20, 30, 30] ,
1989-
"D": ["a", "b", "c", "c"] }
2027+
let data = {
2028+
"A": [-20, 30, 47.3, -20],
2029+
"B": [34, -4, 5, 6],
2030+
"C": [20, 20, 30, 30],
2031+
"D": ["a", "b", "c", "c"]
2032+
}
19902033

19912034
let df = new DataFrame(data, { index: ["a", "b", "c", "d"] })
19922035
df.rename({ mapper: { "a": 0, "b": 1 }, axis: 0, inplace: true })

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)