|
| 1 | +import { Series } from '../../../core/dataframe/Series.js'; |
| 2 | +import { VectorFactory } from '../../../core/storage/VectorFactory.js'; |
| 3 | + |
1 | 4 | /** |
2 | 5 | * Apply a function to each column in a DataFrame |
3 | 6 | * |
4 | | - * @returns {Function} - Function that takes a DataFrame and applies the function to each column |
| 7 | + * @param {DataFrame} df - DataFrame to transform |
| 8 | + * @param {Function} func - Function to apply to each value |
| 9 | + * @param {Object} options - Options for apply |
| 10 | + * @param {boolean} [options.inplace=false] - Whether to modify the DataFrame in place |
| 11 | + * @param {string|string[]} [options.columns] - Columns to apply the function to (default: all columns) |
| 12 | + * @returns {DataFrame} - New DataFrame with transformed values or the original DataFrame if inplace=true |
5 | 13 | */ |
6 | | -export const apply = |
7 | | - () => |
8 | | - (df, func, options = {}) => { |
9 | | - const { inplace = false, columns = df.columns } = options; |
10 | | - |
11 | | - // Validate columns |
12 | | - for (const col of columns) { |
13 | | - if (!df.columns.includes(col)) { |
14 | | - throw new Error(`Column '${col}' not found`); |
15 | | - } |
16 | | - } |
| 14 | +export function apply(df, func, options = {}) { |
| 15 | + const { inplace = false, columns = df.columns } = options; |
17 | 16 |
|
18 | | - // Create a new object to hold the transformed columns |
19 | | - const result = {}; |
| 17 | + // Validate function |
| 18 | + if (typeof func !== 'function') { |
| 19 | + throw new Error('Function to apply must be provided'); |
| 20 | + } |
20 | 21 |
|
21 | | - // Copy columns that are not being transformed |
22 | | - for (const col of df.columns) { |
23 | | - if (!columns.includes(col)) { |
24 | | - result[col] = df.col(col).toArray(); |
25 | | - } |
26 | | - } |
| 22 | + // Convert columns to array if it's a string |
| 23 | + const targetColumns = Array.isArray(columns) ? columns : [columns]; |
27 | 24 |
|
28 | | - // Apply function to specified columns |
29 | | - for (const col of columns) { |
30 | | - const series = df.col(col); |
31 | | - const values = series.toArray(); |
32 | | - result[col] = values.map(func); |
33 | | - } |
| 25 | + // Validate columns |
| 26 | + for (const col of targetColumns) { |
| 27 | + if (!df.columns.includes(col)) { |
| 28 | + throw new Error(`Column '${col}' not found`); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Apply function to specified columns |
| 33 | + if (inplace) { |
| 34 | + // Directly modify the DataFrame's internal structure for inplace |
| 35 | + for (const col of targetColumns) { |
| 36 | + const values = df.col(col).toArray(); |
| 37 | + const transformedValues = values.map((value, index) => { |
| 38 | + const result = func(value, index, col); |
| 39 | + // Преобразуем null и undefined в NaN для соответствия тестам |
| 40 | + return result === null || result === undefined ? NaN : result; |
| 41 | + }); |
| 42 | + |
| 43 | + // Create a new Series for this column |
| 44 | + const vector = VectorFactory.from(transformedValues); |
| 45 | + const series = new Series(vector, { name: col }); |
| 46 | + |
| 47 | + // Update the Series in the DataFrame |
| 48 | + df._columns[col] = series; |
| 49 | + } |
| 50 | + |
| 51 | + return df; |
| 52 | + } else { |
| 53 | + // Create a new object to hold the transformed columns |
| 54 | + const result = {}; |
| 55 | + |
| 56 | + // Copy all columns from the original DataFrame |
| 57 | + for (const col of df.columns) { |
| 58 | + result[col] = df.col(col).toArray(); |
| 59 | + } |
| 60 | + |
| 61 | + // Apply function to specified columns |
| 62 | + for (const col of targetColumns) { |
| 63 | + const values = result[col]; |
| 64 | + result[col] = values.map((value, index) => { |
| 65 | + const result = func(value, index, col); |
| 66 | + // Преобразуем null и undefined в NaN для соответствия тестам |
| 67 | + return result === null || result === undefined ? NaN : result; |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + return new df.constructor(result); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Apply a function to all columns in a DataFrame |
| 77 | + * |
| 78 | + * @param {DataFrame} df - DataFrame to transform |
| 79 | + * @param {Function} func - Function to apply to each value |
| 80 | + * @param {Object} options - Options for applyAll |
| 81 | + * @param {boolean} [options.inplace=false] - Whether to modify the DataFrame in place |
| 82 | + * @returns {DataFrame} - New DataFrame with transformed values or the original DataFrame if inplace=true |
| 83 | + */ |
| 84 | +export function applyAll(df, func, options = {}) { |
| 85 | + // Simply call apply with all columns |
| 86 | + return apply(df, func, { ...options, columns: df.columns }); |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Register apply methods on DataFrame prototype |
| 91 | + * @param {Class} DataFrame - DataFrame class to extend |
| 92 | + */ |
| 93 | +export function register(DataFrame) { |
| 94 | + if (!DataFrame) { |
| 95 | + throw new Error('DataFrame instance is required'); |
| 96 | + } |
34 | 97 |
|
35 | | - // Return new DataFrame or modify in place |
36 | | - if (inplace) { |
37 | | - // Replace columns in original DataFrame |
38 | | - for (const col of columns) { |
39 | | - df._columns[col] = result[col]; |
40 | | - } |
41 | | - return df; |
| 98 | + DataFrame.prototype.apply = function (columns, func, options = {}) { |
| 99 | + // If first argument is a function, assume it's for all columns |
| 100 | + if (typeof columns === 'function') { |
| 101 | + const result = applyAll(this, columns, options); |
| 102 | + if (options.inplace) { |
| 103 | + return this; |
42 | 104 | } |
| 105 | + return result; |
| 106 | + } |
43 | 107 |
|
44 | | - // Create a new DataFrame with the transformed columns |
45 | | - return new df.constructor(result); |
46 | | - }; |
| 108 | + const result = apply(this, func, { ...options, columns }); |
| 109 | + if (options.inplace) { |
| 110 | + return this; |
| 111 | + } |
| 112 | + return result; |
| 113 | + }; |
47 | 114 |
|
48 | | -export default { apply }; |
| 115 | + DataFrame.prototype.applyAll = function (func, options = {}) { |
| 116 | + const result = applyAll(this, func, options); |
| 117 | + if (options.inplace) { |
| 118 | + return this; |
| 119 | + } |
| 120 | + return result; |
| 121 | + }; |
| 122 | +} |
0 commit comments