Skip to content

Commit 6265d36

Browse files
committed
test: Added unit tests for diff() function
1 parent 5d8bd2d commit 6265d36

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/danfojs-node/test/core/frame.test.ts

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

993993
});
994994

995+
describe("diff", function () {
996+
it("Return difference of DataFrame with previous row", function () {
997+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
998+
const df = new DataFrame(data);
999+
assert.deepEqual((df.diff(1) as DataFrame).values, [[NaN, NaN, NaN], [10, 8, 6], [-9, -8, -7]]);
1000+
});
1001+
it("Return difference of DataFrame with following row", function () {
1002+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
1003+
const df = new DataFrame(data);
1004+
assert.deepEqual((df.diff(-1) as DataFrame).values, [[-2, 0, 2], [8, 8, 8], [-1, 0, 1]]);
1005+
});
1006+
it("Return difference of a DataFrame with a Series along default axis 1", function () {
1007+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
1008+
const sf = new Series([1, 2, 1]);
1009+
const df = new DataFrame(data);
1010+
assert.deepEqual((df.diff(sf) as DataFrame).values, [[-1, 0, 3], [9, 8, 9], [0, 0, 2]]);
1011+
});
1012+
it("Return difference of a DataFrame with along axis 0 (column-wise), previous column", function () {
1013+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
1014+
const df = new DataFrame(data);
1015+
assert.deepEqual((df.diff(1, { axis: 0 }) as DataFrame).values, [[NaN, 2, 2], [NaN, 0, 0], [NaN, 1, 1]]);
1016+
});
1017+
it("Return difference of a DataFrame with along axis 0 (column-wise), following column", function () {
1018+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
1019+
const df = new DataFrame(data);
1020+
assert.deepEqual((df.diff(1, { axis: 0 }) as DataFrame).values, [[-2, 2, NaN], [0, 0, NaN], [1, 1, NaN]]);
1021+
});
1022+
it("Return difference of a DataFrame with another DataFrame along default axis 1", function () {
1023+
const df1 = new DataFrame([[0, 2, 4], [3, 10, 4]]);
1024+
const df2 = new DataFrame([[1, 2, 4], [10, 5, 0]]);
1025+
assert.deepEqual((df1.diff(df2) as DataFrame).values, [[-1, 0, 0], [-7, 5, 4]]);
1026+
});
1027+
it("Return difference of a DataFrame with another DataFrame along axis 0", function () {
1028+
const df1 = new DataFrame([[0, 2, 4], [3, 10, 4]]);
1029+
const df2 = new DataFrame([[1, 2, 4], [10, 5, 0]]);
1030+
assert.deepEqual((df1.diff(df2, { axis: 0 }) as DataFrame).values, [[-1, 0, 0], [-7, 5, 4]]);
1031+
});
1032+
});
1033+
9951034
describe("mod", function () {
9961035
it("Return modulus of DataFrame with a single Number", function () {
9971036
const data = [[0, 2, 4], [360, 180, 360]];

0 commit comments

Comments
 (0)