MCPcopy Index your code
hub / github.com/javascriptdata/danfojs / setIndex

Method setIndex

src/danfojs-base/core/frame.ts:2418–2490  ·  view source on GitHub ↗
(
        options:
            {
                index?: Array<number | string | (number | string)>,
                column?: string,
                drop?: boolean,
                inplace?: boolean
            }
    )

Source from the content-addressed store, hash-verified

2416 }
2417 ): DataFrame
2418 setIndex(
2419 options:
2420 {
2421 index?: Array<number | string | (number | string)>,
2422 column?: string,
2423 drop?: boolean,
2424 inplace?: boolean
2425 }
2426 ): DataFrame | void {
2427 const { index, column, drop, inplace } = { drop: false, inplace: false, ...options }
2428
2429 if (!index && !column) {
2430 throw new Error("ParamError: must specify either index or column")
2431 }
2432
2433 let newIndex: Array<string | number> = [];
2434
2435 if (index) {
2436 if (!Array.isArray(index)) {
2437 throw Error(`ParamError: index must be an array`);
2438 }
2439
2440 if (index.length !== this.values.length) {
2441 throw Error(`ParamError: index must be the same length as the number of rows`);
2442 }
2443 newIndex = index;
2444 }
2445
2446 if (column) {
2447 if (this.columns.indexOf(column) === -1) {
2448 throw Error(`ParamError: column not found in column names`);
2449 }
2450
2451 newIndex = this.$getColumnData(column, false) as Array<string | number>
2452 }
2453
2454 if (drop) {
2455 const dfDropped = this.drop({ columns: [column as string] })
2456
2457 const newData = dfDropped?.values as ArrayType2D
2458 const newColumns = dfDropped?.columns
2459 const newDtypes = dfDropped?.dtypes
2460
2461 if (inplace) {
2462 this.$setValues(newData, true, false)
2463 this.$setIndex(newIndex)
2464 this.$setColumnNames(newColumns)
2465 } else {
2466 const df = new DataFrame(newData,
2467 {
2468 index: newIndex,
2469 columns: newColumns,
2470 dtypes: newDtypes,
2471 config: { ...this.config }
2472 });
2473 return df;
2474 }
2475 } else {

Callers

nothing calls this directly

Calls 6

$getColumnDataMethod · 0.95
dropMethod · 0.95
indexOfMethod · 0.80
$setValuesMethod · 0.80
$setIndexMethod · 0.65
$setColumnNamesMethod · 0.65

Tested by

no test coverage detected