From c604929b220c0328f36542dd48d6506ac1b19f8f Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 13 Nov 2025 01:30:37 +1030 Subject: [PATCH] Fixed Replacing a value with 0 with dataframe.replace throws an error Fixes #621 --- src/danfojs-base/core/frame.ts | 18 +++++++++++++----- src/danfojs-base/core/series.ts | 16 ++++++++++++---- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/danfojs-base/core/frame.ts b/src/danfojs-base/core/frame.ts index 2f35c3d5..e57b8ad9 100644 --- a/src/danfojs-base/core/frame.ts +++ b/src/danfojs-base/core/frame.ts @@ -2759,16 +2759,24 @@ export default class DataFrame extends NDframe implements DataFrameInterface { ): DataFrame | void { const { columns, inplace } = { inplace: false, ...options } + if (oldValue === undefined) { + throw Error(`Params Error: Must specify param 'oldValue' to replace`); + } + + if (newValue === undefined) { + throw Error(`Params Error: Must specify param 'newValue' to replace with`); + } + if (typeof oldValue === 'number' && isNaN(oldValue)) { - throw Error(`Params Error: Param 'oldValue' does not support NaN. Use DataFrame.fillNa() instead.`); + throw Error(`Params Error: Param 'oldValue' does not support NaN. Use Series.fillNa() instead.`); } - if (!oldValue && typeof oldValue !== 'boolean' && typeof oldValue !== 'number' && typeof oldValue !== 'string') { - throw Error(`Params Error: Must specify param 'oldValue' to replace`); + if (typeof oldValue !== 'boolean' && typeof oldValue !== 'number' && typeof oldValue !== 'string') { + throw Error(`Params Error: Param 'oldValue' must be of type string or number or boolean.`); } - if (!newValue && typeof newValue !== 'boolean' && typeof newValue !== 'number' && typeof newValue !== 'string') { - throw Error(`Params Error: Must specify param 'newValue' to replace with`); + if (typeof newValue !== 'boolean' && typeof newValue !== 'number' && typeof newValue !== 'string') { + throw Error(`Params Error: Param 'newValue' must be of type string or number or boolean.`); } let newData: ArrayType2D = [] diff --git a/src/danfojs-base/core/series.ts b/src/danfojs-base/core/series.ts index 9db5f37a..7a059aaf 100644 --- a/src/danfojs-base/core/series.ts +++ b/src/danfojs-base/core/series.ts @@ -1536,16 +1536,24 @@ export default class Series extends NDframe implements SeriesInterface { ): Series | void { const { inplace } = { inplace: false, ...options } + if (oldValue === undefined) { + throw Error(`Params Error: Must specify param 'oldValue' to replace`); + } + + if (newValue === undefined) { + throw Error(`Params Error: Must specify param 'newValue' to replace with`); + } + if (typeof oldValue === 'number' && isNaN(oldValue)) { throw Error(`Params Error: Param 'oldValue' does not support NaN. Use Series.fillNa() instead.`); } - if (!oldValue && typeof oldValue !== 'boolean' && typeof oldValue !== 'number' && typeof oldValue !== 'string') { - throw Error(`Params Error: Must specify param 'oldValue' to replace`); + if (typeof oldValue !== 'boolean' && typeof oldValue !== 'number' && typeof oldValue !== 'string') { + throw Error(`Params Error: Param 'oldValue' must be of type string or number or boolean.`); } - if (!newValue && typeof newValue !== 'boolean' && typeof newValue !== 'number' && typeof newValue !== 'string') { - throw Error(`Params Error: Must specify param 'newValue' to replace with`); + if (typeof newValue !== 'boolean' && typeof newValue !== 'number' && typeof newValue !== 'string') { + throw Error(`Params Error: Param 'newValue' must be of type string or number or boolean.`); } const newArr = [...this.values].map((val) => {