Skip to content

Commit 4b685b3

Browse files
committed
feat(test): browser unit tests for diff()
1 parent 63b46e8 commit 4b685b3

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe("DataFrame", function () {
101101
assert.deepEqual(df.dtypes, [ "string", "int32", "float32", "string" ]);
102102
assert.deepEqual(df.index, [ 0, 1, 2, 3 ]);
103103
});
104-
it("Add new Series to DataFrame works", function () {
104+
it("Add new dfd.Series to DataFrame works", function () {
105105
let data = { alpha: [ "A", "B", "C", "D" ], count: [ 1, 2, 3, 4 ], sum: [ 20.3, 30.456, 40.90, 90.1 ] };
106106
let df = new dfd.DataFrame(data);
107107
const newdf = df.addColumn("new_column", new dfd.Series([ "a", "b", "c", "d" ]));
@@ -1018,6 +1018,51 @@ describe("DataFrame", function () {
10181018

10191019
});
10201020

1021+
describe("diff", 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.diff(0)).values, [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ]);
1026+
});
1027+
it("Return difference of DataFrame with previous row", function () {
1028+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];
1029+
const df = new dfd.DataFrame(data);
1030+
assert.deepEqual((df.diff(1)).values, [ [ NaN, NaN, NaN ], [ 10, 8, 6 ], [ -9, -8, -7 ] ]);
1031+
});
1032+
it("Return difference of DataFrame with following row", function () {
1033+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];
1034+
const df = new dfd.DataFrame(data);
1035+
assert.deepEqual((df.diff(-1)).values, [ [ -10, -8, -6 ], [ 9, 8, 7 ], [ NaN, NaN, NaN ] ]);
1036+
});
1037+
it("Return difference 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 dfd.Series([ 1, 2, 1 ]);
1040+
const df = new dfd.DataFrame(data);
1041+
assert.deepEqual((df.diff(sf)).values, [ [ -1, 0, 3 ], [ 9, 8, 9 ], [ 0, 0, 2 ] ]);
1042+
});
1043+
it("Return difference 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.diff(1, { axis: 0 })).values, [ [ NaN, 2, 2 ], [ NaN, 0, 0 ], [ NaN, 1, 1 ] ]);
1047+
});
1048+
it("Return difference of a DataFrame with along axis 0 (column-wise), following column", function () {
1049+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];
1050+
const df = new dfd.DataFrame(data);
1051+
assert.deepEqual((df.diff(-1, { axis: 0 })).values, [ [ -2, -2, NaN ], [ 0, 0, NaN ], [ -1, -1, NaN ] ]);
1052+
});
1053+
it("Return difference 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 ], [ 10, 5, 0 ] ]);
1056+
assert.deepEqual((df1.diff(df2)).values, [ [ 1, 4, 0 ], [ -7, 5, 4 ] ]);
1057+
});
1058+
it("Throw error if DataFrame for diff contains string", function () {
1059+
const df = new dfd.DataFrame([ [ "words", "words", "words" ], [ "words", "words", "words" ] ]);
1060+
assert.throws(() => {
1061+
df.diff(1);
1062+
}, Error, "TypeError: diff 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)