|
| 1 | +// src/methods/filtering/iloc.js |
| 2 | + |
| 3 | +/** |
| 4 | + * Creates a function that selects rows and columns by their integer positions. |
| 5 | + * |
| 6 | + * @param {Object} deps - Dependencies |
| 7 | + * @returns {Function} Function that selects rows and columns by integer positions |
| 8 | + */ |
| 9 | +export const iloc = (deps) => (frame, rowIndices, columnIndices) => { |
| 10 | + // Validate input |
| 11 | + if (!Array.isArray(rowIndices)) { |
| 12 | + rowIndices = [rowIndices]; |
| 13 | + } |
| 14 | + |
| 15 | + if (!Array.isArray(columnIndices)) { |
| 16 | + columnIndices = [columnIndices]; |
| 17 | + } |
| 18 | + |
| 19 | + // Validate that all indices are numbers |
| 20 | + if (!rowIndices.every((idx) => typeof idx === 'number' && idx >= 0)) { |
| 21 | + throw new Error('Row indices must be non-negative numbers'); |
| 22 | + } |
| 23 | + |
| 24 | + if (!columnIndices.every((idx) => typeof idx === 'number' && idx >= 0)) { |
| 25 | + throw new Error('Column indices must be non-negative numbers'); |
| 26 | + } |
| 27 | + |
| 28 | + // Get all column names |
| 29 | + const allColumns = Object.keys(frame.columns); |
| 30 | + |
| 31 | + // Get the number of rows |
| 32 | + const rowCount = frame.columns[allColumns[0]]?.length || 0; |
| 33 | + |
| 34 | + // Check if row indices are valid |
| 35 | + const maxRowIndex = Math.max(...rowIndices); |
| 36 | + if (maxRowIndex >= rowCount) { |
| 37 | + throw new Error( |
| 38 | + `Row index ${maxRowIndex} is out of bounds (0-${rowCount - 1})`, |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // Check if column indices are valid |
| 43 | + const maxColumnIndex = Math.max(...columnIndices); |
| 44 | + if (maxColumnIndex >= allColumns.length) { |
| 45 | + throw new Error( |
| 46 | + `Column index ${maxColumnIndex} is out of bounds (0-${allColumns.length - 1})`, |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + // Map column indices to column names |
| 51 | + const selectedColumns = columnIndices.map((idx) => allColumns[idx]); |
| 52 | + |
| 53 | + // Create a new frame with selected rows and columns |
| 54 | + const result = { |
| 55 | + columns: {}, |
| 56 | + }; |
| 57 | + |
| 58 | + // Initialize columns in the result |
| 59 | + selectedColumns.forEach((column) => { |
| 60 | + result.columns[column] = []; |
| 61 | + }); |
| 62 | + |
| 63 | + // Add selected rows to the result |
| 64 | + rowIndices.forEach((rowIdx) => { |
| 65 | + selectedColumns.forEach((column) => { |
| 66 | + result.columns[column].push(frame.columns[column][rowIdx]); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + // Convert arrays to typed arrays if the original columns were typed |
| 71 | + selectedColumns.forEach((column) => { |
| 72 | + const originalArray = frame.columns[column]; |
| 73 | + if (originalArray instanceof Float64Array) { |
| 74 | + result.columns[column] = new Float64Array(result.columns[column]); |
| 75 | + } else if (originalArray instanceof Int32Array) { |
| 76 | + result.columns[column] = new Int32Array(result.columns[column]); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + return result; |
| 81 | +}; |
0 commit comments