Skip to content

Commit 6fa9cb8

Browse files
committed
Updated Series.replace() and DataFrame.replace() to handle falsy numbers (0), falsy strings (''), and throw when oldValue is NaN.
1 parent 5e26647 commit 6fa9cb8

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-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

0 commit comments

Comments
 (0)