Skip to content

Commit ae20c91

Browse files
authored
Merge pull request #562 from babennettdev/559-replace-does-not-accept-falsy-strings-or-numbers
559 replace does not accept falsy strings or numbers
2 parents c21cabb + 6fa9cb8 commit ae20c91

File tree

4 files changed

+139
-4
lines changed

4 files changed

+139
-4
lines changed

src/danfojs-base/core/frame.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,11 +2759,15 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
27592759
): DataFrame | void {
27602760
const { columns, inplace } = { inplace: false, ...options }
27612761

2762-
if (!oldValue && typeof oldValue !== 'boolean') {
2762+
if (typeof oldValue === 'number' && isNaN(oldValue)) {
2763+
throw Error(`Params Error: Param 'oldValue' does not support NaN. Use DataFrame.fillNa() instead.`);
2764+
}
2765+
2766+
if (!oldValue && typeof oldValue !== 'boolean' && typeof oldValue !== 'number' && typeof oldValue !== 'string') {
27632767
throw Error(`Params Error: Must specify param 'oldValue' to replace`);
27642768
}
27652769

2766-
if (!newValue && typeof newValue !== 'boolean') {
2770+
if (!newValue && typeof newValue !== 'boolean' && typeof newValue !== 'number' && typeof newValue !== 'string') {
27672771
throw Error(`Params Error: Must specify param 'newValue' to replace with`);
27682772
}
27692773

src/danfojs-base/core/series.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,11 +1536,15 @@ export default class Series extends NDframe implements SeriesInterface {
15361536
): Series | void {
15371537
const { inplace } = { inplace: false, ...options }
15381538

1539-
if (!oldValue && typeof oldValue !== 'boolean') {
1539+
if (typeof oldValue === 'number' && isNaN(oldValue)) {
1540+
throw Error(`Params Error: Param 'oldValue' does not support NaN. Use Series.fillNa() instead.`);
1541+
}
1542+
1543+
if (!oldValue && typeof oldValue !== 'boolean' && typeof oldValue !== 'number' && typeof oldValue !== 'string') {
15401544
throw Error(`Params Error: Must specify param 'oldValue' to replace`);
15411545
}
15421546

1543-
if (!newValue && typeof newValue !== 'boolean') {
1547+
if (!newValue && typeof newValue !== 'boolean' && typeof newValue !== 'number' && typeof newValue !== 'string') {
15441548
throw Error(`Params Error: Must specify param 'newValue' to replace with`);
15451549
}
15461550

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,68 @@ 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 does not support NaN", function () {
2167+
const data1 = [[NaN, 19, 84, NaN], [65, NaN, NaN, 37]];
2168+
const df = new DataFrame(data1);
2169+
assert.throws(() => df.replace(NaN, 1), Error, "Params Error: Param 'oldValue' does not support NaN. Use DataFrame.fillNa() instead.");
2170+
});
2171+
2172+
it("Replace oldValue supports falsy strings", function () {
2173+
const data1 = [['', 'hello', 'world', ''], ['foo', '', '', 'bar']];
2174+
const df = new DataFrame(data1);
2175+
const expected = [['danfo', 'hello', 'world', 'danfo'], ['foo', 'danfo', 'danfo', 'bar']];
2176+
const df_rep = df.replace('', 'danfo') as DataFrame;
2177+
assert.deepEqual(df_rep.values, expected);
2178+
});
2179+
2180+
it("Replace oldValue supports falsy booleans", function () {
2181+
const data1 = [[false, 'hello', 'world', false], ['foo', false, false, 'bar']];
2182+
const df = new DataFrame(data1);
2183+
const expected = [[true, 'hello', 'world', true], ['foo', true, true, 'bar']];
2184+
const df_rep = df.replace(false, true) as DataFrame;
2185+
assert.deepEqual(df_rep.values, expected);
2186+
});
2187+
2188+
it("Replace newValue supports falsy numbers (0)", function () {
2189+
const data1 = [[1, 19, 84, 1], [65, 1, 1, 37]];
2190+
const df = new DataFrame(data1);
2191+
const expected = [[0, 19, 84, 0], [65, 0, 0, 37]];
2192+
const df_rep = df.replace(1, 0) as DataFrame;
2193+
assert.deepEqual(df_rep.values, expected);
2194+
});
2195+
2196+
it("Replace newValue supports falsy numbers (NaN)", function () {
2197+
const data1 = [[1, 19, 84, 1], [65, 1, 1, 37]];
2198+
const df = new DataFrame(data1);
2199+
const expected = [[NaN, 19, 84, NaN], [65, NaN, NaN, 37]];
2200+
const df_rep = df.replace(1, NaN) as DataFrame;
2201+
assert.deepEqual(df_rep.values, expected);
2202+
});
2203+
2204+
it("Replace newValue supports falsy strings", function () {
2205+
const data1 = [['danfo', 'hello', 'world', 'danfo'], ['foo', 'danfo', 'danfo', 'bar']];
2206+
const df = new DataFrame(data1);
2207+
const expected = [['', 'hello', 'world', ''], ['foo', '', '', 'bar']];
2208+
const df_rep = df.replace('danfo', '') as DataFrame;
2209+
assert.deepEqual(df_rep.values, expected);
2210+
});
2211+
2212+
it("Replace newValue supports falsy booleans", function () {
2213+
const data1 = [[true, 'hello', 'world', true], ['foo', true, true, 'bar']];
2214+
const df = new DataFrame(data1);
2215+
const expected = [[false, 'hello', 'world', false], ['foo', false, false, 'bar']];
2216+
const df_rep = df.replace(true, false) as DataFrame;
2217+
assert.deepEqual(df_rep.values, expected);
2218+
});
2219+
21582220
});
21592221

21602222
describe("sum", function () {

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,13 +1171,78 @@ describe("Series Functions", () => {
11711171
sf.replace("A", "boy", { inplace: true });
11721172
assert.deepEqual(sf.values, expected);
11731173
});
1174+
11741175
it("Replace values given in replace param with value (boolean type)", function () {
11751176
const data1 = [true, true, false, false];
11761177
const sf = new Series(data1);
11771178
const expected = [false, false, false, false];
11781179
sf.replace(true, false, { inplace: true });
11791180
assert.deepEqual(sf.values, expected);
11801181
});
1182+
1183+
it("Replace oldValue supports falsy numbers (0)", function () {
1184+
const data1 = [0, 45, 56, 25, 23, 20, 0];
1185+
const sf = new Series(data1);
1186+
const expected = [1, 45, 56, 25, 23, 20, 1];
1187+
const dfRep = sf.replace(0, 1)
1188+
assert.deepEqual(dfRep.values, expected);
1189+
});
1190+
1191+
it("Replace oldValue does not support NaN", function () {
1192+
const data1 = [NaN, 45, 56, 25, 23, 20, NaN];
1193+
const sf = new Series(data1);
1194+
assert.throws(() => sf.replace(NaN, 1), Error, "Params Error: Param 'oldValue' does not support NaN. Use Series.fillNa() instead.");
1195+
1196+
});
1197+
1198+
it("Replace oldValue supports falsy strings", function () {
1199+
const data1 = ['', 'bar', 'baz'];
1200+
const sf = new Series(data1);
1201+
const expected = ['foo', 'bar', 'baz'];
1202+
const dfRep = sf.replace('', 'foo')
1203+
assert.deepEqual(dfRep.values, expected);
1204+
});
1205+
1206+
it("Replace oldValue supports falsy booleans", function () {
1207+
const data1 = [true, false, true, false];
1208+
const sf = new Series(data1);
1209+
const expected = [true, true, true, true];
1210+
const dfRep = sf.replace(false, true)
1211+
assert.deepEqual(dfRep.values, expected);
1212+
});
1213+
1214+
it("Replace newValue supports falsy numbers (0)", function () {
1215+
const data1 = [1, 45, 56, 25, 23, 20, 1];
1216+
const sf = new Series(data1);
1217+
const expected = [0, 45, 56, 25, 23, 20, 0];
1218+
const dfRep = sf.replace(1, 0)
1219+
assert.deepEqual(dfRep.values, expected);
1220+
});
1221+
1222+
it("Replace newValue supports falsy numbers (NaN)", function () {
1223+
const data1 = [1, 45, 56, 25, 23, 20, 1];
1224+
const sf = new Series(data1);
1225+
const expected = [NaN, 45, 56, 25, 23, 20, NaN];
1226+
const dfRep = sf.replace(1, NaN)
1227+
assert.deepEqual(dfRep.values, expected);
1228+
});
1229+
1230+
it("Replace newValue supports falsy strings", function () {
1231+
const data1 = ['foo', 'bar', 'baz'];
1232+
const sf = new Series(data1);
1233+
const expected = ['', 'bar', 'baz'];
1234+
const dfRep = sf.replace('foo', '')
1235+
assert.deepEqual(dfRep.values, expected);
1236+
});
1237+
1238+
it("Replace newValue supports falsy booleans", function () {
1239+
const data1 = [true, false, true, false];
1240+
const sf = new Series(data1);
1241+
const expected = [false, false, false, false];
1242+
const dfRep = sf.replace(true, false)
1243+
assert.deepEqual(dfRep.values, expected);
1244+
});
1245+
11811246
// it("Throw error on wrong param passed", function () {
11821247
// const data1 = ["A", "A", "A", "B", "B", "C", "C", "D"];
11831248
// const sf = new Series(data1);

0 commit comments

Comments
 (0)