Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/core/src/methods/dataframe/display/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* DataFrame display methods
*
* This module exports all display methods for DataFrame.
* Methods are registered using extendDataFrame.
*
* @module methods/dataframe/display
*/

import { DataFrame } from '../../../data/model/index.js';
import { extendDataFrame } from '../../../data/model/extendDataFrame.js';
import * as pool from './pool.js';

extendDataFrame(DataFrame.prototype, pool); // without namespace — base display methods

// export directly (so that you can call display(df) if needed)
export * from './pool.js';
33 changes: 19 additions & 14 deletions packages/core/src/methods/dataframe/filtering/expr$.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,46 +47,52 @@ export function expr$(df, strings, ...values) {
if (filteredRows.length === 0) {
// Create a new DataFrame instance with the same options as the original
const result = new df.constructor({}, df._options);

// For each column, create a Series with the appropriate type
for (const col of allColumns) {
// Get the original column data to determine its type
const originalColumn = df._columns[col];
const originalArray = originalColumn.vector.__data;

// Create an empty array with the same type
if (ArrayBuffer.isView(originalArray) && !(originalArray instanceof DataView)) {
if (
ArrayBuffer.isView(originalArray) &&
!(originalArray instanceof DataView)
) {
const TypedArrayConstructor = originalArray.constructor;
const emptyTypedArray = new TypedArrayConstructor(0);
result._columns[col] = createTypedSeries(emptyTypedArray, col, df);
} else {
result._columns[col] = createTypedSeries([], col, df);
}

// Add to column order
if (!result._order.includes(col)) {
result._order.push(col);
}
}

return result;
}

// For non-empty results, create a new DataFrame with filtered rows
// Create a new DataFrame instance with the same options as the original
const result = new df.constructor({}, df._options);

// For each column, create a Series with the appropriate type
for (const col of allColumns) {
// Get the original column data to determine its type
const originalColumn = df._columns[col];
const originalArray = originalColumn.vector.__data;

// Extract values for this column from the filtered rows
const values = filteredRows.map(row => row[col]);
const values = filteredRows.map((row) => row[col]);

// Preserve the array type if it's a typed array
if (ArrayBuffer.isView(originalArray) && !(originalArray instanceof DataView)) {
if (
ArrayBuffer.isView(originalArray) &&
!(originalArray instanceof DataView)
) {
const TypedArrayConstructor = originalArray.constructor;
const typedValues = new TypedArrayConstructor(values.length);
values.forEach((value, i) => {
Expand All @@ -96,19 +102,19 @@ export function expr$(df, strings, ...values) {
} else {
result._columns[col] = createTypedSeries(values, col, df);
}

// Add to column order
if (!result._order.includes(col)) {
result._order.push(col);
}
}

return result;
}

/**
* Create a predicate function for filtering rows
*
*
* @param {string} expr - Expression to evaluate
* @returns {Function} - Predicate function
* @private
Expand All @@ -134,4 +140,3 @@ function createPredicate(expr) {
}

// Export the expr$ method directly
export { expr$ };
29 changes: 17 additions & 12 deletions packages/core/src/methods/dataframe/filtering/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,50 @@ export function filter(df, predicate) {
if (filteredRows.length === 0) {
// Create a new DataFrame instance with the same options as the original
const result = new df.constructor({}, df._options);

// For each column, create a Series with the appropriate type
for (const col of allColumns) {
// Get the original column data to determine its type
const originalColumn = df._columns[col];
const originalArray = originalColumn.vector.__data;

// Create an empty array with the same type
if (ArrayBuffer.isView(originalArray) && !(originalArray instanceof DataView)) {
if (
ArrayBuffer.isView(originalArray) &&
!(originalArray instanceof DataView)
) {
const TypedArrayConstructor = originalArray.constructor;
const emptyTypedArray = new TypedArrayConstructor(0);
result._columns[col] = createTypedSeries(emptyTypedArray, col, df);
} else {
result._columns[col] = createTypedSeries([], col, df);
}

// Add to column order
if (!result._order.includes(col)) {
result._order.push(col);
}
}

return result;
}

// For non-empty results, create a new DataFrame with filtered rows
// Create a new DataFrame instance with the same options as the original
const result = new df.constructor({}, df._options);

// For each column, create a Series with the appropriate type
for (const col of allColumns) {
// Get the original column data to determine its type
const originalColumn = df._columns[col];
const originalArray = originalColumn.vector.__data;
const values = filteredRows.map(row => row[col]);
const values = filteredRows.map((row) => row[col]);

// Preserve the array type if it's a typed array
if (ArrayBuffer.isView(originalArray) && !(originalArray instanceof DataView)) {
if (
ArrayBuffer.isView(originalArray) &&
!(originalArray instanceof DataView)
) {
const TypedArrayConstructor = originalArray.constructor;
const typedValues = new TypedArrayConstructor(values.length);
values.forEach((value, i) => {
Expand All @@ -78,15 +84,14 @@ export function filter(df, predicate) {
} else {
result._columns[col] = createTypedSeries(values, col, df);
}

// Add to column order
if (!result._order.includes(col)) {
result._order.push(col);
}
}

return result;
}

// Export the filter method directly
export { filter };
31 changes: 18 additions & 13 deletions packages/core/src/methods/dataframe/filtering/iloc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*-------------------------------------------------------------------------*
| DataFrame -› filtering · iloc() |
| |
| Выбор строк и колонок из DataFrame по целочисленным позициям. |
| Selection of rows and columns from DataFrame by integer positions. |
| |
| df.iloc(5) → выбор строки с индексом 5 |
| df.iloc([1, 3, 5]) → выбор строк с указанными индексами |
| df.iloc(5, 2) → выбор значения в строке 5, колонке 2 |
| df.iloc([1, 3], [0, 2]) → выбор строк 1,3 и колонок 0,2 |
| df.iloc(5) → select row with index 5 |
| df.iloc([1, 3, 5]) → select rows with specified indices |
| df.iloc(5, 2) → select value in row 5, column 2 |
| df.iloc([1, 3], [0, 2]) → select rows 1,3 and columns 0,2 |
*-------------------------------------------------------------------------*/

/**
Expand Down Expand Up @@ -75,7 +75,10 @@ export function iloc(df, rowSelector = null, colSelector = null) {
// Process column selector
if (colSelector === null || colSelector === undefined) {
// If selector is null, select all columns
selectedColumnIndices = Array.from({ length: allColumns.length }, (_, i) => i);
selectedColumnIndices = Array.from(
{ length: allColumns.length },
(_, i) => i,
);
} else if (typeof colSelector === 'number') {
// Single column index
const idx = colSelector < 0 ? allColumns.length + colSelector : colSelector;
Expand Down Expand Up @@ -118,16 +121,19 @@ export function iloc(df, rowSelector = null, colSelector = null) {

// Create a new DataFrame instance with the same options as the original
const result = new df.constructor({}, df._options);

// For each selected column, create a Series with the appropriate type
for (const col of selectedColumns) {
// Get the original column data to determine its type
const originalColumn = df._columns[col];
const originalArray = originalColumn.vector.__data;
const values = selectedIndices.map(index => rows[index][col]);
const values = selectedIndices.map((index) => rows[index][col]);

// Preserve the array type if it's a typed array
if (ArrayBuffer.isView(originalArray) && !(originalArray instanceof DataView)) {
if (
ArrayBuffer.isView(originalArray) &&
!(originalArray instanceof DataView)
) {
const TypedArrayConstructor = originalArray.constructor;
const typedValues = new TypedArrayConstructor(values.length);
values.forEach((value, i) => {
Expand All @@ -137,15 +143,14 @@ export function iloc(df, rowSelector = null, colSelector = null) {
} else {
result._columns[col] = createTypedSeries(values, col, df);
}

// Add to column order
if (!result._order.includes(col)) {
result._order.push(col);
}
}

return result;
}

// Export the method for the pool
export default { iloc };
Loading
Loading