From ec1b16db9f8c538e7e09faf249950920ca806323 Mon Sep 17 00:00:00 2001 From: Rising Odegua Date: Sun, 10 Oct 2021 20:59:10 +0100 Subject: [PATCH] Minor bug fixes --- danfojs-browser/src/core/frame.js | 5 +++-- danfojs-browser/src/core/series.js | 7 ++++--- danfojs-browser/tests/core/frame.js | 11 +++++++++++ danfojs-node/src/core/frame.js | 5 +++-- danfojs-node/src/core/series.js | 7 ++++--- danfojs-node/tests/core/frame.js | 24 +++++++++++++++++++++++- 6 files changed, 48 insertions(+), 11 deletions(-) diff --git a/danfojs-browser/src/core/frame.js b/danfojs-browser/src/core/frame.js index 7e38aff9..af460e9b 100644 --- a/danfojs-browser/src/core/frame.js +++ b/danfojs-browser/src/core/frame.js @@ -1237,7 +1237,7 @@ export default class DataFrame extends NDframe { const dfValues = this.values; for (let i = 0; i < dfValues.length; i++) { const values = dfValues[i]; - if (!values.includes(NaN)) { + if (!values.includes(NaN) && !values.includes(undefined) && !values.includes(null)) { newData.push(values); newIndex.push(this.index[i]); } @@ -2100,8 +2100,9 @@ export default class DataFrame extends NDframe { }); const sortedObjectArr = utils.sortObj(objToSort, ascending); - const sortedIndex = sortedObjectArr.map((obj) => obj.index); + let sortedIndex = sortedObjectArr.map((obj) => obj.index); const newData = sortedIndex.map((i) => (this.values)[i]); + sortedIndex = sortedIndex.map((i) => index[i]); if (inplace) { this.$setValues(newData); diff --git a/danfojs-browser/src/core/series.js b/danfojs-browser/src/core/series.js index 9e9cacb1..94e40313 100644 --- a/danfojs-browser/src/core/series.js +++ b/danfojs-browser/src/core/series.js @@ -789,9 +789,10 @@ export default class Series extends NDframe { if (inplace) { this.$setValues(data); } else { - const sf = this.copy(); - sf.$setValues(data); - return sf; + return new Series(data, { + index: this.index, + config: { ...this.config } + }); } } diff --git a/danfojs-browser/tests/core/frame.js b/danfojs-browser/tests/core/frame.js index 8e5f30c9..9bd09650 100644 --- a/danfojs-browser/tests/core/frame.js +++ b/danfojs-browser/tests/core/frame.js @@ -2047,6 +2047,17 @@ describe("DataFrame", function () { let rslt = [[2, 4, 6, 'c'], [0, 2, 4, 'b'], [360, 180, 360, 'a']]; assert.deepEqual(df.values, rslt); }); + it("sort index in descending order and retains index", function () { + let data = [[0, 2, 4, "b"], + [360, 180, 360, "a"], + [2, 4, 6, "c"]]; + + let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"], index: ["b", "a", "c"] }); + let df2 = df.sort_index({ ascending: false }); + let rslt = ["c", "b", "a"]; + + assert.deepEqual(df2.index, rslt); + }); }); describe("append", function () { diff --git a/danfojs-node/src/core/frame.js b/danfojs-node/src/core/frame.js index 889e88c3..9a768eab 100644 --- a/danfojs-node/src/core/frame.js +++ b/danfojs-node/src/core/frame.js @@ -1239,7 +1239,7 @@ export default class DataFrame extends NDframe { const dfValues = this.values; for (let i = 0; i < dfValues.length; i++) { const values = dfValues[i]; - if (!values.includes(NaN)) { + if (!values.includes(NaN) && !values.includes(undefined) && !values.includes(null)) { newData.push(values); newIndex.push(this.index[i]); } @@ -2102,8 +2102,9 @@ export default class DataFrame extends NDframe { }); const sortedObjectArr = utils.sortObj(objToSort, ascending); - const sortedIndex = sortedObjectArr.map((obj) => obj.index); + let sortedIndex = sortedObjectArr.map((obj) => obj.index); const newData = sortedIndex.map((i) => (this.values)[i]); + sortedIndex = sortedIndex.map((i) => index[i]); if (inplace) { this.$setValues(newData); diff --git a/danfojs-node/src/core/series.js b/danfojs-node/src/core/series.js index 09f3e88c..42555905 100644 --- a/danfojs-node/src/core/series.js +++ b/danfojs-node/src/core/series.js @@ -785,9 +785,10 @@ export default class Series extends NDframe { if (inplace) { this.$setValues(data); } else { - const sf = this.copy(); - sf.$setValues(data); - return sf; + return new Series(data, { + index: this.index, + config: { ...this.config } + }); } } diff --git a/danfojs-node/tests/core/frame.js b/danfojs-node/tests/core/frame.js index 463e4980..7e00dff2 100644 --- a/danfojs-node/tests/core/frame.js +++ b/danfojs-node/tests/core/frame.js @@ -644,7 +644,7 @@ describe("DataFrame", function () { let data = [[0, 2, 4], [360, 180, 360]]; let df = new DataFrame(data, { columns: ["col1", "col2", "col3"] }); assert.deepEqual(df.mean().values, [180, 91, 182]); - assert.deepEqual(df.mean().index, ["col1", "col2", "col3"] ); + assert.deepEqual(df.mean().index, ["col1", "col2", "col3"]); }); it("Return mean of a DataFrame along axis 0 (row)", function () { @@ -1431,6 +1431,17 @@ describe("DataFrame", function () { assert.deepEqual(df.values, df_val); }); + it("drop works for undefined values", function () { + let data = [[null, 1, 2, 3], [3, 4, undefined, 9], [5, 6, 7, 8]]; + let column = ["A", "B", "C", "D"]; + let df = new DataFrame(data, { columns: column }); + + let df_val = [[5, 6, 7, 8]]; + + df.dropna(0, { inplace: true }); + assert.deepEqual(df.values, df_val); + + }); }); describe("isna", function () { @@ -2068,6 +2079,17 @@ describe("DataFrame", function () { let rslt = [[2, 4, 6, 'c'], [0, 2, 4, 'b'], [360, 180, 360, 'a']]; assert.deepEqual(df.values, rslt); }); + it("sort index in descending order and retains index", function () { + let data = [[0, 2, 4, "b"], + [360, 180, 360, "a"], + [2, 4, 6, "c"]]; + + let df = new DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"], index: ["b", "a", "c"] }); + let df2 = df.sort_index({ ascending: false }); + let rslt = ["c", "b", "a"]; + + assert.deepEqual(df2.index, rslt); + }); }); describe("append", function () {