| 46 | * @param options.config General configuration object for extending or setting NDframe behavior. |
| 47 | */ |
| 48 | export default class DataFrame extends NDframe implements DataFrameInterface { |
| 49 | [key: string]: any |
| 50 | constructor(data: any, options: BaseDataOptionType = {}) { |
| 51 | const { index, columns, dtypes, config } = options; |
| 52 | super({ data, index, columns, dtypes, config, isSeries: false }); |
| 53 | this.$setInternalColumnDataProperty(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Maps all column names to their corresponding data, and return them as Series objects. |
| 58 | * This makes column subsetting works. E.g this can work ==> `df["col1"]` |
| 59 | * @param column Optional, a single column name to map |
| 60 | */ |
| 61 | private $setInternalColumnDataProperty(column?: string) { |
| 62 | const self = this; |
| 63 | if (column && typeof column === "string") { |
| 64 | Object.defineProperty(self, column, { |
| 65 | get() { |
| 66 | return self.$getColumnData(column) |
| 67 | }, |
| 68 | set(arr: ArrayType1D | Series) { |
| 69 | self.$setColumnData(column, arr); |
| 70 | } |
| 71 | }) |
| 72 | } else { |
| 73 | const columns = this.columns; |
| 74 | for (let i = 0; i < columns.length; i++) { |
| 75 | const column = columns[i]; |
| 76 | Object.defineProperty(this, column, { |
| 77 | get() { |
| 78 | return self.$getColumnData(column) |
| 79 | }, |
| 80 | set(arr: ArrayType1D | Series) { |
| 81 | self.$setColumnData(column, arr); |
| 82 | } |
| 83 | }) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the column data from the DataFrame by column name. |
| 91 | * @param column column name to get the column data |
| 92 | * @param returnSeries Whether to return the data in series format or not. Defaults to true |
| 93 | */ |
| 94 | private $getColumnData(column: string, returnSeries: boolean = true) { |
| 95 | const columnIndex = this.columns.indexOf(column) |
| 96 | |
| 97 | if (columnIndex == -1) { |
| 98 | ErrorThrower.throwColumnNotFoundError(this) |
| 99 | } |
| 100 | |
| 101 | const dtypes = [this.$dtypes[columnIndex]] |
| 102 | const index = [...this.$index] |
| 103 | const columns = [column] |
| 104 | const config = { ...this.$config } |
| 105 |
nothing calls this directly
no outgoing calls
no test coverage detected