Skip to content

Commit 7200a40

Browse files
committed
Added additonal tests for DataFrame.replace()
1 parent f272a7e commit 7200a40

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,70 @@ describe("DataFrame", function () {
21552155
assert.deepEqual(df.values, expected);
21562156
});
21572157

2158+
it("Replace oldValue supports falsy numbers (0)", function () {
2159+
const data1 = [[0, 19, 84, 0], [65, 0, 0, 37]];
2160+
const df = new DataFrame(data1);
2161+
const expected = [[1, 19, 84, 1], [65, 1, 1, 37]];
2162+
const df_rep = df.replace(0, 1) as DataFrame;
2163+
assert.deepEqual(df_rep.values, expected);
2164+
});
2165+
2166+
it("Replace oldValue supports falsy numbers (NaN)", function () {
2167+
const data1 = [[NaN, 19, 84, NaN], [65, NaN, NaN, 37]];
2168+
const df = new DataFrame(data1);
2169+
const expected = [[1, 19, 84, 1], [65, 1, 1, 37]];
2170+
const df_rep = df.replace(NaN, 1) as DataFrame;
2171+
assert.deepEqual(df_rep.values, expected);
2172+
});
2173+
2174+
it("Replace oldValue supports falsy strings", function () {
2175+
const data1 = [['', 'hello', 'world', ''], ['foo', '', '', 'bar']];
2176+
const df = new DataFrame(data1);
2177+
const expected = [['danfo', 'hello', 'world', 'danfo'], ['foo', 'danfo', 'danfo', 'bar']];
2178+
const df_rep = df.replace('', 'danfo') as DataFrame;
2179+
assert.deepEqual(df_rep.values, expected);
2180+
});
2181+
2182+
it("Replace oldValue supports falsy booleans", function () {
2183+
const data1 = [[false, 'hello', 'world', false], ['foo', false, false, 'bar']];
2184+
const df = new DataFrame(data1);
2185+
const expected = [[true, 'hello', 'world', true], ['foo', true, true, 'bar']];
2186+
const df_rep = df.replace(false, true) as DataFrame;
2187+
assert.deepEqual(df_rep.values, expected);
2188+
});
2189+
2190+
it("Replace newValue supports falsy numbers (0)", function () {
2191+
const data1 = [[1, 19, 84, 1], [65, 1, 1, 37]];
2192+
const df = new DataFrame(data1);
2193+
const expected = [[0, 19, 84, 0], [65, 0, 0, 37]];
2194+
const df_rep = df.replace(1, 0) as DataFrame;
2195+
assert.deepEqual(df_rep.values, expected);
2196+
});
2197+
2198+
it("Replace newValue supports falsy numbers (NaN)", function () {
2199+
const data1 = [[1, 19, 84, 1], [65, 1, 1, 37]];
2200+
const df = new DataFrame(data1);
2201+
const expected = [[NaN, 19, 84, NaN], [65, NaN, NaN, 37]];
2202+
const df_rep = df.replace(1, NaN) as DataFrame;
2203+
assert.deepEqual(df_rep.values, expected);
2204+
});
2205+
2206+
it("Replace newValue supports falsy strings", function () {
2207+
const data1 = [['danfo', 'hello', 'world', 'danfo'], ['foo', 'danfo', 'danfo', 'bar']];
2208+
const df = new DataFrame(data1);
2209+
const expected = [['', 'hello', 'world', ''], ['foo', '', '', 'bar']];
2210+
const df_rep = df.replace('danfo', '') as DataFrame;
2211+
assert.deepEqual(df_rep.values, expected);
2212+
});
2213+
2214+
it("Replace newValue supports falsy booleans", function () {
2215+
const data1 = [[true, 'hello', 'world', true], ['foo', true, true, 'bar']];
2216+
const df = new DataFrame(data1);
2217+
const expected = [[false, 'hello', 'world', false], ['foo', false, false, 'bar']];
2218+
const df_rep = df.replace(true, false) as DataFrame;
2219+
assert.deepEqual(df_rep.values, expected);
2220+
});
2221+
21582222
});
21592223

21602224
describe("sum", function () {

0 commit comments

Comments
 (0)