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

Method addColumn

src/danfojs-base/core/frame.ts:1940–2009  ·  view source on GitHub ↗
(
        column: string,
        values: Series | ArrayType1D,
        options?: { inplace?: boolean, atIndex?: number | string }
    )

Source from the content-addressed store, hash-verified

1938 options?: { inplace?: boolean, atIndex?: number | string }
1939 ): DataFrame
1940 addColumn(
1941 column: string,
1942 values: Series | ArrayType1D,
1943 options?: { inplace?: boolean, atIndex?: number | string }
1944 ): DataFrame | void {
1945 let { inplace, atIndex } = { inplace: false, atIndex: this.columns.length, ...options };
1946 if (typeof atIndex === "string") {
1947 if (!(this.columns.includes(atIndex))) {
1948 throw new Error(`${atIndex} not a column`)
1949 }
1950 atIndex = this.columns.indexOf(atIndex)
1951 }
1952
1953 if (!column) {
1954 throw new Error("ParamError: column must be specified")
1955 }
1956
1957 if (!values) {
1958 throw new Error("ParamError: values must be specified")
1959 }
1960
1961 const columnIndex = this.$columns.indexOf(column)
1962
1963 if (columnIndex === -1) {
1964 let colunmValuesToAdd: ArrayType1D
1965
1966 if (values instanceof Series) {
1967 colunmValuesToAdd = values.values as ArrayType1D
1968 } else if (Array.isArray(values)) {
1969 colunmValuesToAdd = values;
1970 } else {
1971 throw new Error("ParamError: specified value not supported. It must either be an Array or a Series of the same length")
1972 }
1973
1974 if (colunmValuesToAdd.length !== this.shape[0]) {
1975 ErrorThrower.throwColumnLengthError(this, colunmValuesToAdd.length)
1976 }
1977
1978 const newData = []
1979 const oldValues = this.$data
1980 for (let i = 0; i < oldValues.length; i++) {
1981 const innerArr = [...oldValues[i]] as ArrayType1D
1982 innerArr.splice(atIndex, 0, colunmValuesToAdd[i])
1983 newData.push(innerArr)
1984 }
1985
1986 if (inplace) {
1987 this.$setValues(newData, true, false)
1988 let columns = [...this.columns]
1989 columns.splice(atIndex, 0, column)
1990 this.$setColumnNames(columns)
1991 this.$setInternalColumnDataProperty(column);
1992
1993 } else {
1994 let columns = [...this.columns]
1995 columns.splice(atIndex, 0, column)
1996
1997 const df = new DataFrame(newData, {

Callers

nothing calls this directly

Calls 7

$setColumnDataMethod · 0.95
includesMethod · 0.80
indexOfMethod · 0.80
$setValuesMethod · 0.80
inferDtypeMethod · 0.80
$setColumnNamesMethod · 0.65

Tested by

no test coverage detected