Search in sources :

Example 6 with Column

use of tech.tablesaw.columns.Column in project symja_android_library by axkr.

the class StandardTableSliceGroup method splitOn.

/**
 * Splits the sourceTable table into sub-tables, grouping on the columns whose names are given in
 * splitColumnNames
 */
private void splitOn(String... splitColumnNames) {
    Map<ByteArray, Selection> selectionMap = new LinkedHashMap<>();
    Map<ByteArray, String> sliceNameMap = new HashMap<>();
    List<Column<?>> splitColumns = getSourceTable().columns(splitColumnNames);
    if (containsTextColumn(splitColumns)) {
        for (int i = 0; i < getSourceTable().rowCount(); i++) {
            ByteArrayList byteArrayList = new ByteArrayList();
            StringBuilder stringKey = new StringBuilder();
            int count = 0;
            for (Column<?> col : splitColumns) {
                stringKey.append(col.getString(i));
                if (count < splitColumns.size() - 1) {
                    stringKey.append(SPLIT_STRING);
                }
                byteArrayList.addElements(byteArrayList.size(), col.asBytes(i));
                count++;
            }
            // Add to the matching selection.
            ByteArray byteArray = new ByteArray(byteArrayList.toByteArray());
            Selection selection = selectionMap.getOrDefault(byteArray, new BitmapBackedSelection());
            selection.add(i);
            selectionMap.put(byteArray, selection);
            sliceNameMap.put(byteArray, stringKey.toString());
        }
    } else {
        // handle the case where split is on non-text-columns
        int byteSize = getByteSize(splitColumns);
        for (int i = 0; i < getSourceTable().rowCount(); i++) {
            StringBuilder stringKey = new StringBuilder();
            ByteBuffer byteBuffer = ByteBuffer.allocate(byteSize);
            int count = 0;
            for (Column<?> col : splitColumns) {
                stringKey.append(col.getString(i));
                if (count < splitColumns.size() - 1) {
                    stringKey.append(SPLIT_STRING);
                }
                byteBuffer.put(col.asBytes(i));
                count++;
            }
            // Add to the matching selection.
            ByteArray byteArray = new ByteArray(byteBuffer.array());
            Selection selection = selectionMap.getOrDefault(byteArray, new BitmapBackedSelection());
            selection.add(i);
            selectionMap.put(byteArray, selection);
            sliceNameMap.put(byteArray, stringKey.toString());
        }
    }
    // Construct slices for all the values in our maps
    for (Entry<ByteArray, Selection> entry : selectionMap.entrySet()) {
        TableSlice slice = new TableSlice(getSourceTable(), entry.getValue());
        slice.setName(sliceNameMap.get(entry.getKey()));
        addSlice(slice);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection) Selection(tech.tablesaw.selection.Selection) ByteBuffer(java.nio.ByteBuffer) LinkedHashMap(java.util.LinkedHashMap) Column(tech.tablesaw.columns.Column) CategoricalColumn(tech.tablesaw.api.CategoricalColumn) ByteArrayList(it.unimi.dsi.fastutil.bytes.ByteArrayList) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection)

Example 7 with Column

use of tech.tablesaw.columns.Column in project symja_android_library by axkr.

the class DataFrameJoiner method crossProduct.

/**
 * Creates cross product for the selection of two tables.
 *
 * @param destination the destination table.
 * @param table1 the table on left of join.
 * @param table2 the table on right of join.
 * @param table1Rows the selection of rows in table1.
 * @param table2Rows the selection of rows in table2.
 * @param ignoreColumns a set of column indexes in the result to ignore. They are redundant join
 *     columns.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void crossProduct(Table destination, Table table1, Table table2, Selection table1Rows, Selection table2Rows, Set<Integer> ignoreColumns, boolean keepTable2JoinKeyColumns) {
    for (int c = 0; c < table1.columnCount() + table2.columnCount(); c++) {
        if (!keepTable2JoinKeyColumns && ignoreColumns.contains(c)) {
            continue;
        }
        int table2Index = c - table1.columnCount();
        for (int r1 : table1Rows) {
            for (int r2 : table2Rows) {
                if (c < table1.columnCount()) {
                    Column t1Col = table1.column(c);
                    destination.column(c).append(t1Col, r1);
                } else {
                    Column t2Col = table2.column(table2Index);
                    destination.column(c).append(t2Col, r2);
                }
            }
        }
    }
}
Also used : Column(tech.tablesaw.columns.Column)

Example 8 with Column

use of tech.tablesaw.columns.Column in project symja_android_library by axkr.

the class TableSliceGroup method aggregate.

/**
 * Applies the given aggregations to the given columns. The apply and combine steps of a
 * split-apply-combine.
 *
 * @param functions map from column name to aggregation to apply on that function
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Table aggregate(ListMultimap<String, AggregateFunction<?, ?>> functions) {
    Table groupTable = summaryTableName(sourceTable);
    StringColumn groupColumn = StringColumn.create("Group");
    groupTable.addColumns(groupColumn);
    boolean firstFunction = true;
    for (Map.Entry<String, Collection<AggregateFunction<?, ?>>> entry : functions.asMap().entrySet()) {
        String columnName = entry.getKey();
        for (AggregateFunction function : entry.getValue()) {
            String colName = aggregateColumnName(columnName, function.functionName());
            ColumnType type = function.returnType();
            Column resultColumn = type.create(colName);
            for (TableSlice subTable : getSlices()) {
                Object result = function.summarize(subTable.column(columnName));
                if (firstFunction) {
                    groupColumn.append(subTable.name());
                }
                if (function.returnType().equals(ColumnType.DOUBLE)) {
                    Number number = (Number) result;
                    resultColumn.append(number.doubleValue());
                } else {
                    resultColumn.append(result);
                }
            }
            groupTable.addColumns(resultColumn);
            firstFunction = false;
        }
    }
    return splitGroupingColumn(groupTable);
}
Also used : StringColumn(tech.tablesaw.api.StringColumn) Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) StringColumn(tech.tablesaw.api.StringColumn) Column(tech.tablesaw.columns.Column) AggregateFunction(tech.tablesaw.aggregate.AggregateFunction) Collection(java.util.Collection) Map(java.util.Map)

Example 9 with Column

use of tech.tablesaw.columns.Column in project symja_android_library by axkr.

the class XlsxReader method appendValue.

@SuppressWarnings("unchecked")
private Column<?> appendValue(Column<?> column, Cell cell) {
    CellType cellType = cell.getCellType() == FORMULA ? cell.getCachedFormulaResultType() : cell.getCellType();
    switch(cellType) {
        case STRING:
            column.appendCell(cell.getRichStringCellValue().getString());
            return null;
        case NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                // This will return inconsistent results across time zones, but that matches Excel's
                // behavior
                LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                if (column.type() == ColumnType.STRING) {
                    // If column has String type try to honor it and leave the value as an string as similar
                    // as posible as seen in Excel
                    String dataFormatStyle = cell.getCellStyle().getDataFormatString();
                    String val;
                    if ("general".equalsIgnoreCase(dataFormatStyle)) {
                        val = new CellGeneralFormatter().format(cell.getNumericCellValue());
                    } else {
                        val = new CellDateFormatter(dataFormatStyle).format(cell.getDateCellValue());
                    }
                    column.appendCell(val);
                } else {
                    column.appendCell(localDate.toString());
                }
                return null;
            } else {
                double num = cell.getNumericCellValue();
                if (column.type() == ColumnType.INTEGER) {
                    Column<Integer> intColumn = (Column<Integer>) column;
                    if ((int) num == num) {
                        intColumn.append((int) num);
                        return null;
                    } else if ((long) num == num) {
                        Column<Long> altColumn = LongColumn.create(column.name(), column.size());
                        altColumn = intColumn.mapInto(s -> (long) s, altColumn);
                        altColumn.append((long) num);
                        return altColumn;
                    } else {
                        Column<Double> altColumn = DoubleColumn.create(column.name(), column.size());
                        altColumn = intColumn.mapInto(s -> (double) s, altColumn);
                        altColumn.append(num);
                        return altColumn;
                    }
                } else if (column.type() == ColumnType.LONG) {
                    Column<Long> longColumn = (Column<Long>) column;
                    if ((long) num == num) {
                        longColumn.append((long) num);
                        return null;
                    } else {
                        Column<Double> altColumn = DoubleColumn.create(column.name(), column.size());
                        altColumn = longColumn.mapInto(s -> (double) s, altColumn);
                        altColumn.append(num);
                        return altColumn;
                    }
                } else if (column.type() == ColumnType.DOUBLE) {
                    Column<Double> doubleColumn = (Column<Double>) column;
                    doubleColumn.append(num);
                    return null;
                } else if (column.type() == ColumnType.STRING) {
                    // If column has String type try to honor it and leave the value as an string as similar
                    // as posible as seen in Excel
                    Column<String> stringColumn = (Column<String>) column;
                    String dataFormatStyle = cell.getCellStyle().getDataFormatString();
                    String val;
                    if ("general".equalsIgnoreCase(dataFormatStyle)) {
                        val = new CellGeneralFormatter().format(cell.getNumericCellValue());
                    } else {
                        val = new CellNumberFormatter(dataFormatStyle).format(cell.getNumericCellValue());
                    }
                    stringColumn.append(val);
                }
            }
            break;
        case BOOLEAN:
            if (column.type() == ColumnType.BOOLEAN) {
                Column<Boolean> booleanColumn = (Column<Boolean>) column;
                booleanColumn.append(cell.getBooleanCellValue());
                return null;
            } else if (column.type() == ColumnType.STRING) {
                // If column has String type try to honor it and leave the value as an string as similar
                // as posible as seen in Excel
                Column<String> stringColumn = (Column<String>) column;
                String val = new CellGeneralFormatter().format(cell.getBooleanCellValue());
                stringColumn.append(val);
            }
        default:
            break;
    }
    return null;
}
Also used : LocalDateTime(java.time.LocalDateTime) CellNumberFormatter(org.apache.poi.ss.format.CellNumberFormatter) Date(java.util.Date) CellDateFormatter(org.apache.poi.ss.format.CellDateFormatter) CellType(org.apache.poi.ss.usermodel.CellType) LongColumn(tech.tablesaw.api.LongColumn) DoubleColumn(tech.tablesaw.api.DoubleColumn) Column(tech.tablesaw.columns.Column) CellGeneralFormatter(org.apache.poi.ss.format.CellGeneralFormatter)

Example 10 with Column

use of tech.tablesaw.columns.Column in project symja_android_library by axkr.

the class Table method transpose.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Table transpose(Table transposed, ColumnType resultColumnType, IntFunction<String> columnNameExtractor, int startingColumn) {
    for (int row = 0; row < this.rowCount(); row++) {
        String columnName = columnNameExtractor.apply(row);
        Column column = resultColumnType.create(columnName);
        for (int col = startingColumn; col < this.columnCount(); col++) {
            column.append(this.column(col), row);
        }
        transposed.addColumns(column);
    }
    return transposed;
}
Also used : Column(tech.tablesaw.columns.Column)

Aggregations

Column (tech.tablesaw.columns.Column)17 ArrayList (java.util.ArrayList)7 Table (tech.tablesaw.api.Table)7 ColumnType (tech.tablesaw.api.ColumnType)6 Row (tech.tablesaw.api.Row)4 List (java.util.List)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 ExprColumn (tech.tablesaw.api.ExprColumn)3 Streams (com.google.common.collect.Streams)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 Beta (com.google.common.annotations.Beta)1 Objects (com.google.common.base.Objects)1 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1