MCPcopy Index your code
hub / github.com/javascriptdata/danfojs / $setColumnData

Method $setColumnData

src/danfojs-base/core/frame.ts:145–186  ·  view source on GitHub ↗

* Updates the internal column data via column name. * @param column The name of the column to update. * @param arr The new column data

(column: string, arr: ArrayType1D | Series)

Source from the content-addressed store, hash-verified

143 * @param arr The new column data
144 */
145 private $setColumnData(column: string, arr: ArrayType1D | Series): void {
146
147 const columnIndex = this.$columns.indexOf(column)
148
149 if (columnIndex == -1) {
150 throw new Error(`ParamError: column ${column} not found in ${this.$columns}. If you need to add a new column, use the df.addColumn method. `)
151 }
152
153 let colunmValuesToAdd: ArrayType1D
154
155 if (arr instanceof Series) {
156 colunmValuesToAdd = arr.values as ArrayType1D
157 } else if (Array.isArray(arr)) {
158 colunmValuesToAdd = arr;
159 } else {
160 throw new Error("ParamError: specified value not supported. It must either be an Array or a Series of the same length")
161 }
162
163 if (colunmValuesToAdd.length !== this.shape[0]) {
164 ErrorThrower.throwColumnLengthError(this, colunmValuesToAdd.length)
165 }
166
167 if (this.$config.isLowMemoryMode) {
168 //Update row ($data) array
169 for (let i = 0; i < this.$data.length; i++) {
170 (this.$data as any)[i][columnIndex] = colunmValuesToAdd[i]
171 }
172 //Update the dtypes
173 this.$dtypes[columnIndex] = utils.inferDtype(colunmValuesToAdd)[0]
174 } else {
175 //Update row ($data) array
176 for (let i = 0; i < this.values.length; i++) {
177 (this.$data as any)[i][columnIndex] = colunmValuesToAdd[i]
178 }
179 //Update column ($dataIncolumnFormat) array since it's available in object
180 (this.$dataIncolumnFormat as any)[columnIndex] = arr
181
182 //Update the dtypes
183 this.$dtypes[columnIndex] = utils.inferDtype(colunmValuesToAdd)[0]
184 }
185
186 }
187
188 /**
189 * Return data with missing values removed from a specified axis

Callers 2

addColumnMethod · 0.95
setMethod · 0.80

Calls 2

indexOfMethod · 0.80
inferDtypeMethod · 0.80

Tested by

no test coverage detected