Skip to content

Commit e1e7f60

Browse files
committed
feat(test): browser unit tests for pctChange()
1 parent 2c7d9dc commit e1e7f60

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

src/danfojs-browser/tests/core/frame.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,51 @@ describe("DataFrame", function () {
10181018

10191019
});
10201020

1021+
describe("pctChange", function () {
1022+
it("Return same DataFrame if other === 0", function () {
1023+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];
1024+
const df = new dfd.DataFrame(data);
1025+
assert.deepEqual((df.pctChange(0)).values, [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ]);
1026+
});
1027+
it("Return difference in percentage of DataFrame with previous row", function () {
1028+
const data = [ [ 90 ], [ 900 ], [ 900 ] ];
1029+
const df = new dfd.DataFrame(data);
1030+
assert.deepEqual((df.pctChange(1)).values, [ [ NaN ], [ 9 ], [ 0 ] ]);
1031+
});
1032+
it("Return difference in percentage of DataFrame with following row", function () {
1033+
const data = [ [ 0, 5, 15 ], [ 10, 10, 10 ], [ 1, 2, 5 ] ];
1034+
const df = new dfd.DataFrame(data);
1035+
assert.deepEqual((df.pctChange(-1)).values, [ [ -1, -0.5, 0.5 ], [ 9, 4, 1 ], [ NaN, NaN, NaN ] ]);
1036+
});
1037+
it("Return difference in percentage of a DataFrame with a Series along default axis 1", function () {
1038+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];
1039+
const sf = new Series([ 1, 2, 1 ]);
1040+
const df = new dfd.DataFrame(data);
1041+
assert.deepEqual((df.pctChange(sf)).values, [ [ -1, 0, 3 ], [ 9, 4, 9 ], [ 0, 0, 2 ] ]);
1042+
});
1043+
it("Return difference in percentage of a DataFrame with along axis 0 (column-wise), previous column", function () {
1044+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];
1045+
const df = new dfd.DataFrame(data);
1046+
assert.deepEqual((df.pctChange(1, { axis: 0 })).values, [ [ NaN, -1, 1 ], [ NaN, 0, 0 ], [ NaN, 1, 0.5 ] ]);
1047+
});
1048+
it("Return difference in percentage of a DataFrame with along axis 0 (column-wise), following column", function () {
1049+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 8 ] ];
1050+
const df = new dfd.DataFrame(data);
1051+
assert.deepEqual((df.pctChange(-1, { axis: 0 })).values, [ [ -1, -0.5, NaN ], [ 0, 0, NaN ], [ -0.5, -0.75, NaN ] ]);
1052+
});
1053+
it("Return difference in percentage of a DataFrame with another DataFrame along default axis 1", function () {
1054+
const df1 = new dfd.DataFrame([ [ 0, 2, 4 ], [ 3, 10, 4 ] ]);
1055+
const df2 = new dfd.DataFrame([ [ -1, -2, 4 ], [ 6, 5, 0 ] ]);
1056+
assert.deepEqual((df1.pctChange(df2)).values, [ [ -1, -2, 0 ], [ -0.5, 1, -1 ] ]);
1057+
});
1058+
it("Throw error if DataFrame for pctChange contains string", function () {
1059+
const df = new dfd.DataFrame([ [ "words", "words", "words" ], [ "words", "words", "words" ] ]);
1060+
assert.throws(() => {
1061+
df.pctChange(1);
1062+
}, Error, "TypeError: pctChange operation is not supported for string dtypes");
1063+
});
1064+
});
1065+
10211066
describe("mean", function () {
10221067
it("Returns the mean of a DataFrame (Default axis is [1:column])", function () {
10231068
const data = [ [ 0, 2, 4 ],

0 commit comments

Comments
 (0)