MCPcopy Create free account
hub / github.com/javascriptdata/danfojs / replace

Method replace

src/danfojs-base/core/frame.ts:2752–2820  ·  view source on GitHub ↗
(
        oldValue: number | string | boolean,
        newValue: number | string | boolean,
        options?: {
            columns?: Array<string>
            inplace?: boolean
        }
    )

Source from the content-addressed store, hash-verified

2750 }
2751 ): DataFrame
2752 replace(
2753 oldValue: number | string | boolean,
2754 newValue: number | string | boolean,
2755 options?: {
2756 columns?: Array<string>
2757 inplace?: boolean
2758 }
2759 ): DataFrame | void {
2760 const { columns, inplace } = { inplace: false, ...options }
2761
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') {
2767 throw Error(`Params Error: Must specify param 'oldValue' to replace`);
2768 }
2769
2770 if (!newValue && typeof newValue !== 'boolean' && typeof newValue !== 'number' && typeof newValue !== 'string') {
2771 throw Error(`Params Error: Must specify param 'newValue' to replace with`);
2772 }
2773
2774 let newData: ArrayType2D = []
2775
2776 if (columns) {
2777 if (!Array.isArray(columns)) {
2778 throw Error(`Params Error: column must be an array of column(s)`);
2779 }
2780 const columnIndex: Array<number> = []
2781
2782 columns.forEach(column => {
2783 const _indx = this.columns.indexOf(column)
2784 if (_indx === -1) {
2785 throw Error(`Params Error: column not found in columns`);
2786 }
2787 columnIndex.push(_indx)
2788 })
2789
2790 newData = (this.values as ArrayType2D).map(([...row]) => {
2791 for (const colIndx of columnIndex) {
2792 if (row[colIndx] === oldValue) {
2793 row[colIndx] = newValue;
2794 }
2795 }
2796 return row;
2797 })
2798 } else {
2799 newData = (this.values as ArrayType2D).map(([...row]) => {
2800 return row.map((cell => {
2801 if (cell === oldValue) {
2802 return newValue
2803 } else {
2804 return cell
2805 }
2806 }))
2807 })
2808 }
2809

Callers

nothing calls this directly

Calls 3

indexOfMethod · 0.80
$setValuesMethod · 0.80
mapMethod · 0.65

Tested by

no test coverage detected