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

Method rename

src/danfojs-base/core/frame.ts:2965–3026  ·  view source on GitHub ↗
(
        mapper: {
            [index: string | number]: string | number
        },
        options?: {
            axis?: 0 | 1
            inplace?: boolean
        }
    )

Source from the content-addressed store, hash-verified

2963 }
2964 ): DataFrame
2965 rename(
2966 mapper: {
2967 [index: string | number]: string | number
2968 },
2969 options?: {
2970 axis?: 0 | 1
2971 inplace?: boolean
2972 }
2973 ): DataFrame | void {
2974 const { axis, inplace } = { axis: 1, inplace: false, ...options }
2975
2976 if ([0, 1].indexOf(axis) === -1) {
2977 throw Error(`ParamError: axis must be 0 or 1`);
2978 }
2979
2980 if (axis === 1) {
2981 const colsAdded: string[] = [];
2982 const newColumns = this.columns.map(col => {
2983 if (mapper[col] !== undefined) {
2984 const newCol = `${mapper[col]}`;
2985 colsAdded.push(newCol);
2986 return newCol;
2987 } else {
2988 return col
2989 }
2990 })
2991
2992 if (inplace) {
2993 this.$setColumnNames(newColumns)
2994 for (const col of colsAdded) {
2995 this.$setInternalColumnDataProperty(col);
2996 }
2997 } else {
2998 return new DataFrame([...this.values], {
2999 index: [...this.index],
3000 columns: newColumns,
3001 dtypes: [...this.dtypes],
3002 config: { ...this.config }
3003 })
3004 }
3005 } else {
3006 const newIndex = this.index.map(col => {
3007 if (mapper[col] !== undefined) {
3008 return mapper[col]
3009 } else {
3010 return col
3011 }
3012 })
3013
3014 if (inplace) {
3015 this.$setIndex(newIndex)
3016 } else {
3017 return new DataFrame([...this.values], {
3018 index: newIndex,
3019 columns: [...this.columns],
3020 dtypes: [...this.dtypes],
3021 config: { ...this.config }
3022 })

Callers

nothing calls this directly

Calls 5

indexOfMethod · 0.80
mapMethod · 0.65
$setColumnNamesMethod · 0.65
$setIndexMethod · 0.65

Tested by

no test coverage detected