Skip to content

Commit 0baf658

Browse files
committed
test: correct and complete unit test of diff()
Should be about as near full coverage as possible
1 parent 95a2060 commit 0baf658

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,11 @@ describe("DataFrame", function () {
993993
});
994994

995995
describe("diff", function () {
996+
it("Return same DataFrame if other === 0", function () {
997+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
998+
const df = new DataFrame(data);
999+
assert.deepEqual((df.diff(0) as DataFrame).values, [[0, 2, 4], [10, 10, 10], [1, 2, 3]]);
1000+
});
9961001
it("Return difference of DataFrame with previous row", function () {
9971002
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
9981003
const df = new DataFrame(data);
@@ -1001,7 +1006,7 @@ describe("DataFrame", function () {
10011006
it("Return difference of DataFrame with following row", function () {
10021007
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
10031008
const df = new DataFrame(data);
1004-
assert.deepEqual((df.diff(-1) as DataFrame).values, [[-2, 0, 2], [8, 8, 8], [-1, 0, 1]]);
1009+
assert.deepEqual((df.diff(-1) as DataFrame).values, [[-10, -8, -6], [9, 8, 7], [NaN, NaN, NaN]]);
10051010
});
10061011
it("Return difference of a DataFrame with a Series along default axis 1", function () {
10071012
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
@@ -1017,17 +1022,18 @@ describe("DataFrame", function () {
10171022
it("Return difference of a DataFrame with along axis 0 (column-wise), following column", function () {
10181023
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
10191024
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]]);
1025+
assert.deepEqual((df.diff(-1, { axis: 0 }) as DataFrame).values, [[-2, -2, NaN], [0, 0, NaN], [-1, -1, NaN]]);
10211026
});
10221027
it("Return difference of a DataFrame with another DataFrame along default axis 1", function () {
10231028
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]]);
1029+
const df2 = new DataFrame([[-1, -2, 4], [10, 5, 0]]);
1030+
assert.deepEqual((df1.diff(df2) as DataFrame).values, [[1, 4, 0], [-7, 5, 4]]);
1031+
});
1032+
it("Throw error if DataFrame for diff contains string", function () {
1033+
const df = new DataFrame([["words", "words", "words"], ["words", "words", "words"]]);
1034+
assert.throws(() => {
1035+
df.diff(1);
1036+
}, Error, "TypeError: diff operation is not supported for string dtypes");
10311037
});
10321038
});
10331039

0 commit comments

Comments
 (0)