From dfbf67319986c0376ae4860da39980c6a7e1cbfc Mon Sep 17 00:00:00 2001 From: Rising Odegua Date: Wed, 12 Jan 2022 17:00:06 +0100 Subject: [PATCH 1/3] update test sample files --- src/danfojs-node/test/samples/sampleOut.xlsx | Bin 15384 -> 15384 bytes src/danfojs-node/test/samples/test.xlsx | Bin 14964 -> 14964 bytes src/danfojs-node/test/samples/testSeries.xlsx | Bin 15439 -> 15439 bytes 3 files changed, 0 insertions(+), 0 deletions(-) diff --git a/src/danfojs-node/test/samples/sampleOut.xlsx b/src/danfojs-node/test/samples/sampleOut.xlsx index 3f1f1c5328d5eae925efbd2bc6922dadb0e10b2b..30b62258322203f2e2cd4a60f3f9c7b03e62b68a 100644 GIT binary patch delta 148 zcmbPHF{6Sfz?+$civa}K+c)yGGcvKaZ=T8+#{{CkGJjz0RUA)E0zEN delta 148 zcmbPHF{6Sfz?+$civa{Anl|#ZGcrjuZJx>)#{{CkGJjz0RWEiEN%b* diff --git a/src/danfojs-node/test/samples/test.xlsx b/src/danfojs-node/test/samples/test.xlsx index 409e43c12261946099e601dba1e2cb74c7976d77..4e6839ec57f03d554763f4915da027a596066322 100644 GIT binary patch delta 148 zcmexT@}-0)z?+$civa}K+c)yGGcvKaZ=T8+#{{CkGEZd&(LLO;aGct)cZJx>)#{{CkGEZd&(LLn+a delta 148 zcmX?KalV2lz?+$civa{Anl|#ZGcrjuZJx>)#{{CkGV8E_Xl0IEu=pfSUO5ndq55%F z5S^uakR3$pnihlAFEURB(UUo?Y(bQVl?{k$vvL7ZXRNHja@O`B%FEgkM72VA$E?jk Iy#Lk#021{pQ2+n{ From c5c0a0a5f47cb6af5affc7c7ce16bf1767982a35 Mon Sep 17 00:00:00 2001 From: Rising Odegua Date: Sun, 20 Feb 2022 09:27:30 +0100 Subject: [PATCH 2/3] Fix issue #390 --- src/danfojs-base/core/frame.ts | 21 ++++++++++---- src/danfojs-base/core/series.ts | 19 ++++++++++++ src/danfojs-browser/tests/core/frame.test.js | 25 ++++++++-------- src/danfojs-browser/tests/core/series.test.js | 26 ++++++++--------- src/danfojs-node/test/core/frame.test.ts | 24 +++++++-------- src/danfojs-node/test/core/series.test.ts | 29 ++++++++++--------- 6 files changed, 84 insertions(+), 60 deletions(-) diff --git a/src/danfojs-base/core/frame.ts b/src/danfojs-base/core/frame.ts index 00932099..7b82c736 100644 --- a/src/danfojs-base/core/frame.ts +++ b/src/danfojs-base/core/frame.ts @@ -525,12 +525,16 @@ export default class DataFrame extends NDframe implements DataFrameInterface { * ``` */ head(rows: number = 5): DataFrame { - if (rows > this.shape[0]) { - throw new Error("ParamError: Number of rows cannot be greater than available rows in data") - } + if (rows <= 0) { throw new Error("ParamError: Number of rows cannot be less than 1") } + if (this.shape[0] <= rows) { + return this.copy() + } + if (this.shape[0] - rows < 0) { + throw new Error("ParamError: Number of rows cannot be greater than available rows in data") + } return this.iloc({ rows: [`0:${rows}`] }) } @@ -545,12 +549,17 @@ export default class DataFrame extends NDframe implements DataFrameInterface { * ``` */ tail(rows: number = 5): any { - if (rows > this.shape[0]) { - throw new Error("ParamError: Number of rows cannot be greater than available rows in data") - } + if (rows <= 0) { throw new Error("ParamError: Number of rows cannot be less than 1") } + if (this.shape[0] <= rows) { + return this.copy() + } + if (this.shape[0] - rows < 0) { + throw new Error("ParamError: Number of rows cannot be greater than available rows in data") + } + rows = this.shape[0] - rows return this.iloc({ rows: [`${rows}:`] }) } diff --git a/src/danfojs-base/core/series.ts b/src/danfojs-base/core/series.ts index c8a9a9f8..44ba31a2 100644 --- a/src/danfojs-base/core/series.ts +++ b/src/danfojs-base/core/series.ts @@ -162,6 +162,15 @@ export default class Series extends NDframe implements SeriesInterface { * ``` */ head(rows: number = 5): Series { + if (rows <= 0) { + throw new Error("ParamError: Number of rows cannot be less than 1") + } + if (this.shape[0] <= rows) { + return this.copy() + } + if (this.shape[0] - rows < 0) { + throw new Error("ParamError: Number of rows cannot be greater than available rows in data") + } return this.iloc([`0:${rows}`]) } @@ -176,6 +185,16 @@ export default class Series extends NDframe implements SeriesInterface { * ``` */ tail(rows: number = 5): Series { + if (rows <= 0) { + throw new Error("ParamError: Number of rows cannot be less than 1") + } + if (this.shape[0] <= rows) { + return this.copy() + } + if (this.shape[0] - rows < 0) { + throw new Error("ParamError: Number of rows cannot be greater than available rows in data") + } + const startIdx = this.shape[0] - rows return this.iloc([`${startIdx}:`]) } diff --git a/src/danfojs-browser/tests/core/frame.test.js b/src/danfojs-browser/tests/core/frame.test.js index 515f7f07..cbfa0441 100644 --- a/src/danfojs-browser/tests/core/frame.test.js +++ b/src/danfojs-browser/tests/core/frame.test.js @@ -304,19 +304,19 @@ describe("DataFrame", function () { const df = new dfd.DataFrame(data, { columns: cols }); assert.deepEqual(df.head(2).values, [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]); }); - it("Throws error if row specified is greater than values", function () { + it("Throws error if row specified is less than 0", function () { const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ]; const cols = [ "A", "B", "C" ]; const df = new dfd.DataFrame(data, { columns: cols }); - assert.throws(() => df.head(10), Error, - "ParamError: Number of rows cannot be greater than available rows in data"); + assert.throws(() => df.head(-1), Error, + "ParamError: Number of rows cannot be less than 1"); }); - it("Throws error if row specified is less than 0", function () { + + it("Returns all rows when size of DataFrame is less than default (5)", function () { const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ]; const cols = [ "A", "B", "C" ]; const df = new dfd.DataFrame(data, { columns: cols }); - assert.throws(() => df.head(-1), Error, - "ParamError: Number of rows cannot be less than 1"); + assert.deepEqual(df.head().values, data); }); }); @@ -328,13 +328,6 @@ describe("DataFrame", function () { const df = new dfd.DataFrame(data, { columns: cols }); assert.deepEqual(df.tail(2).values, [ [ 20, 30, 40 ], [ 39, 89, 78 ] ]); }); - it("Throws error if row specified is greater than values", function () { - const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ]; - const cols = [ "A", "B", "C" ]; - const df = new dfd.DataFrame(data, { columns: cols }); - assert.throws(() => df.tail(10), Error, - "ParamError: Number of rows cannot be greater than available rows in data"); - }); it("Throws error if row specified is less than 0", function () { const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ]; const cols = [ "A", "B", "C" ]; @@ -353,6 +346,12 @@ describe("DataFrame", function () { const df = new dfd.DataFrame(data); assert.deepEqual(df.tail(2).values, [ [ 1, 2, 34, 5, 0, 6, 4, 5, 6, 7 ], [ 20, 30, 40, 39, 89, 78, 45, 56, 56, 45 ] ]); }); + it("Returns all rows when size of DataFrame is less than default (5)", function () { + const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ]; + const cols = [ "A", "B", "C" ]; + const df = new dfd.DataFrame(data, { columns: cols }); + assert.deepEqual(df.tail().values, data); + }); }); describe("sample", function () { diff --git a/src/danfojs-browser/tests/core/series.test.js b/src/danfojs-browser/tests/core/series.test.js index 23b6ce7f..c4d4e1d3 100644 --- a/src/danfojs-browser/tests/core/series.test.js +++ b/src/danfojs-browser/tests/core/series.test.js @@ -10,17 +10,16 @@ describe("Series Functions", () => { assert.deepEqual(sf.head(2).values, [ 1, 2 ]); assert.deepEqual(sf.head(5).values, [ 1, 2, 3, 4, 5 ]); }); - it("throw error when row specified is greater than values", function () { - const data = [ "Boy", "Girl", "Man", "Woman", "Tall" ]; - const cols = [ "Items" ]; - const sf = new dfd.Series(data, { columns: cols }); - assert.throws(function () { assert.deepEqual(sf.head(10).values, data); }, Error, `row slice [end] index cannot be bigger than 5`); - }); + it("Returns all rows when DataFrame length is less than rows", function () { + const data = [ 1, 2, 5 ]; + const sf = new dfd.Series(data); + assert.deepEqual(sf.head().values, data); + }); it("throw error when row specified is less than 0", function () { const data = [ 1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78 ]; const sf = new dfd.Series(data); - assert.throws(function () { assert.deepEqual(sf.head(-1).values, data); }, Error, `ParamError: end must be greater than start`); + assert.throws(function () { assert.deepEqual(sf.head(-1).values, data); }, Error, `ParamError: Number of rows cannot be less than 1`); }); }); @@ -33,18 +32,17 @@ describe("Series Functions", () => { assert.deepEqual(sf.tail(4).values, [ 40, 39, 89, 78 ]); }); - it("throw error when row specified is greater than values", function () { - const data = [ "Boy", "Girl", "Man", "Woman", "Tall" ]; - const cols = [ "Items" ]; - const sf = new dfd.Series(data, { columns: cols }); - assert.throws(function () { assert.deepEqual(sf.tail(15).values, data); }, Error, `row slice [start] index cannot be less than 0`); - }); + it("Returns all rows when DataFrame length is less than rows", function () { + const data = [ 1, 2, 5 ]; + const sf = new dfd.Series(data); + assert.deepEqual(sf.tail().values, data); + }); it("throw error when row specified is less than 0", function () { const data = [ "Boy", "Girl", "Man", "Woman", "Tall" ]; const cols = [ "Items" ]; const sf = new dfd.Series(data, { columns: cols }); - assert.throws(function () { assert.deepEqual(sf.tail(-1).values, data); }, Error, `ParamError: end must be greater than start`); + assert.throws(function () { assert.deepEqual(sf.tail(-1).values, data); }, Error, `ParamError: Number of rows cannot be less than 1`); }); }); diff --git a/src/danfojs-node/test/core/frame.test.ts b/src/danfojs-node/test/core/frame.test.ts index 76604f60..d8b00d8e 100644 --- a/src/danfojs-node/test/core/frame.test.ts +++ b/src/danfojs-node/test/core/frame.test.ts @@ -308,19 +308,18 @@ describe("DataFrame", function () { const df = new DataFrame(data, { columns: cols }); assert.deepEqual(df.head(2).values, [[1, 2, 3], [4, 5, 6]]); }); - it("Throws error if row specified is greater than values", function () { + it("Throws error if row specified is less than 0", function () { const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]; const cols = ["A", "B", "C"]; const df = new DataFrame(data, { columns: cols }); - assert.throws(() => df.head(10), Error, - "ParamError: Number of rows cannot be greater than available rows in data"); + assert.throws(() => df.head(-1), Error, + "ParamError: Number of rows cannot be less than 1"); }); - it("Throws error if row specified is less than 0", function () { + it("Returns all rows when size of DataFrame is less than default (5)", function () { const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]; const cols = ["A", "B", "C"]; const df = new DataFrame(data, { columns: cols }); - assert.throws(() => df.head(-1), Error, - "ParamError: Number of rows cannot be less than 1"); + assert.deepEqual(df.head().values, data); }); }); @@ -332,13 +331,6 @@ describe("DataFrame", function () { const df = new DataFrame(data, { columns: cols }); assert.deepEqual(df.tail(2).values, [[20, 30, 40], [39, 89, 78]]); }); - it("Throws error if row specified is greater than values", function () { - const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]; - const cols = ["A", "B", "C"]; - const df = new DataFrame(data, { columns: cols }); - assert.throws(() => df.tail(10), Error, - "ParamError: Number of rows cannot be greater than available rows in data"); - }); it("Throws error if row specified is less than 0", function () { const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]; const cols = ["A", "B", "C"]; @@ -357,6 +349,12 @@ describe("DataFrame", function () { const df = new DataFrame(data); assert.deepEqual(df.tail(2).values, [[1, 2, 34, 5, 0, 6, 4, 5, 6, 7], [20, 30, 40, 39, 89, 78, 45, 56, 56, 45]]); }); + it("Returns all rows when size of DataFrame is less than default (5)", function () { + const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]; + const cols = ["A", "B", "C"]; + const df = new DataFrame(data, { columns: cols }); + assert.deepEqual(df.tail().values, data); + }); }); describe("sample", function () { diff --git a/src/danfojs-node/test/core/series.test.ts b/src/danfojs-node/test/core/series.test.ts index 5dadec40..cb25844c 100644 --- a/src/danfojs-node/test/core/series.test.ts +++ b/src/danfojs-node/test/core/series.test.ts @@ -12,18 +12,18 @@ describe("Series Functions", () => { assert.deepEqual(sf.head(2).values, [1, 2]); assert.deepEqual(sf.head(5).values, [1, 2, 3, 4, 5]); }); - it("throw error when row specified is greater than values", function () { - const data = ["Boy", "Girl", "Man", "Woman", "Tall"]; - const cols = ["Items"]; - const sf = new Series(data, { columns: cols }); - assert.throws(function () { assert.deepEqual(sf.head(10).values, data) }, Error, `row slice [end] index cannot be bigger than 5`); - }); it("throw error when row specified is less than 0", function () { const data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78]; const sf = new Series(data); - assert.throws(function () { assert.deepEqual(sf.head(-1).values, data) }, Error, `ParamError: end must be greater than start`); + assert.throws(function () { assert.deepEqual(sf.head(-1).values, data) }, Error, `ParamError: Number of rows cannot be less than 1`); }); + it("Returns all rows when DataFrame length is less than rows", function () { + const data = [ 1, 2, 5 ]; + const sf = new Series(data); + assert.deepEqual(sf.head().values, data); + + }); }); @@ -35,19 +35,20 @@ describe("Series Functions", () => { assert.deepEqual(sf.tail(4).values, [40, 39, 89, 78]); }); - it("throw error when row specified is greater than values", function () { - const data = ["Boy", "Girl", "Man", "Woman", "Tall"]; - const cols = ["Items"]; - const sf = new Series(data, { columns: cols }); - assert.throws(function () { assert.deepEqual(sf.tail(15).values, data) }, Error, `row slice [start] index cannot be less than 0`); - }); it("throw error when row specified is less than 0", function () { const data = ["Boy", "Girl", "Man", "Woman", "Tall"]; const cols = ["Items"]; const sf = new Series(data, { columns: cols }); - assert.throws(function () { assert.deepEqual(sf.tail(-1).values, data) }, Error, `ParamError: end must be greater than start`); + assert.throws(function () { assert.deepEqual(sf.tail(-1).values, data) }, Error, `ParamError: Number of rows cannot be less than 1`); }); + + it("Returns all rows when DataFrame length is less than rows", function () { + const data = [ 1, 2, 5 ]; + const sf = new Series(data); + assert.deepEqual(sf.tail().values, data); + + }); }); describe("sample", function () { From a919675b166e624dc0393c03ae9a5538540fce9d Mon Sep 17 00:00:00 2001 From: Rising Odegua Date: Sun, 20 Feb 2022 10:19:18 +0100 Subject: [PATCH 3/3] Fixes issue #376 and #379 --- src/danfojs-base/shared/utils.ts | 3 +- .../tests/core/generic.test.js | 13 ++++ src/danfojs-node/test/core/generic.test.ts | 77 ++++--------------- 3 files changed, 28 insertions(+), 65 deletions(-) diff --git a/src/danfojs-base/shared/utils.ts b/src/danfojs-base/shared/utils.ts index e24192af..18acaa22 100644 --- a/src/danfojs-base/shared/utils.ts +++ b/src/danfojs-base/shared/utils.ts @@ -334,7 +334,8 @@ export default class Utils { if ( typeof arr[0] == "number" || typeof arr[0] == "string" || - typeof arr[0] == "boolean" + typeof arr[0] == "boolean" || + arr[0] === null ) { return true; } else { diff --git a/src/danfojs-browser/tests/core/generic.test.js b/src/danfojs-browser/tests/core/generic.test.js index 554d8408..0e6f64ee 100644 --- a/src/danfojs-browser/tests/core/generic.test.js +++ b/src/danfojs-browser/tests/core/generic.test.js @@ -74,6 +74,19 @@ describe("Generic (NDFrame)", function () { "IndexError: Row index must contain unique values"; }); }); + + it("Successfully create a 2D Frame when first value is empty", function () { + let data = [ [ null, 20, 1 ], [ 20, 25, 3 ] ]; + let ndframe = new dfd.NDframe({ data, isSeries: false }); + //@ts-ignore + assert.deepEqual(ndframe.values, data); + }); + it("Successfully create a 1D Frame when first value is empty", function () { + let data = [ null, 'bval2', 'bval3', 'bval4' ]; + let ndframe = new dfd.NDframe({ data, isSeries: true }); + //@ts-ignore + assert.deepEqual(ndframe.values, data); + }); }); describe("NDframe Created from JavaScript Object", function () { diff --git a/src/danfojs-node/test/core/generic.test.ts b/src/danfojs-node/test/core/generic.test.ts index ddd09a37..5d8059fe 100644 --- a/src/danfojs-node/test/core/generic.test.ts +++ b/src/danfojs-node/test/core/generic.test.ts @@ -77,6 +77,19 @@ describe("Generic (NDFrame)", function () { "IndexError: Row index must contain unique values" }); }); + + it("Successfully create a 2D Frame when first value is empty", function () { + let data = [[null, 20, 1], [20, 25, 3]]; + let ndframe = new NDframe({ data, isSeries: false }); + //@ts-ignore + assert.deepEqual(ndframe.values, data); + }); + it("Successfully create a 1D Frame when first value is empty", function () { + let data = [null, 'bval2', 'bval3', 'bval4']; + let ndframe = new NDframe({ data, isSeries: true }); + //@ts-ignore + assert.deepEqual(ndframe.values, data); + }); }) describe("NDframe Created from JavaScript Object", function () { @@ -123,70 +136,6 @@ describe("Generic (NDFrame)", function () { let ndframe = new NDframe({ data, isSeries: false }); assert.deepEqual(ndframe.values as any, [["A", NaN], [NaN, 2]]); }); - // it("NDframe created from json takes key position into consideration", function () { - // let json_data = [{ A: "A", B: "B", C: "C" }, - // { A: "A", B: "B", C: "C" }, - // { C: "C", B: "B", A: "A" }, - // { A: "A", C: "C", B: "B" }]; - - // let output = [ - // [ - // 'A', - // 'B', - // 'C' - // ], - // [ - // 'A', - // 'B', - // 'C' - // ], - // [ - // 'A', - // 'B', - // 'C' - // ], - // [ - // 'A', - // 'B', - // 'C' - // ] - // ]; - // let ndframe = new NDframe({ data: json_data, isSeries: false }); - // assert.deepEqual(ndframe.values, output); - // }); - - // it("NDframe created from json sets value to NaN if not present", function () { - // let json_data = [{ A: "A", B: "B", C: "C" }, - // { A: "A", B: "B", C: "C" }, - // { C: "C", B: "B", A: "A" }, - // { A: "A", C: "C" }]; - - // let output = [ - // [ - // 'A', - // 'B', - // 'C' - // ], - // [ - // 'A', - // 'B', - // 'C' - // ], - // [ - // 'A', - // 'B', - // 'C' - // ], - // [ - // 'A', - // 'B', - // NaN - // ] - // ]; - // let ndframe = new NDframe({ data: json_data, isSeries: false }); - // assert.deepEqual(ndframe.values, output); - // }); - }); describe("Replacing row data", function () {