tech.tablesaw.columns.Column Java Examples

The following examples show how to use tech.tablesaw.columns.Column. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Summarizer.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private ArrayListMultimap<String, AggregateFunction<?, ?>> getAggregateFunctionMultimap() {
  ArrayListMultimap<String, AggregateFunction<?, ?>> reductionMultimap =
      ArrayListMultimap.create();
  for (String name : summarizedColumns) {
    Column<?> column = temp.column(name);
    ColumnType type = column.type();
    for (AggregateFunction<?, ?> reduction : reductions) {
      if (reduction.isCompatibleColumn(type)) {
        reductionMultimap.put(name, reduction);
      }
    }
  }
  if (reductionMultimap.isEmpty()) {
    throw new IllegalArgumentException(
        "None of the aggregate functions provided apply to the summarized column type(s).");
  }
  return reductionMultimap;
}
 
Example #2
Source File: Relation.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the NumberColumn at the given index. If the index points to a String or a boolean
 * column, a new NumberColumn is created and returned TODO(lwhite):Consider separating the indexed
 * access and the column type mods, which must be for ML functions (in smile or elsewhere)
 *
 * @param columnIndex The 0-based index of a column in the table
 * @return A number column
 * @throws ClassCastException if the cast to NumberColumn fails
 */
public NumericColumn<?> numberColumn(int columnIndex) {
  Column<?> c = column(columnIndex);
  if (c.type() == ColumnType.STRING) {
    return ((StringColumn) c).asDoubleColumn();
  } else if (c.type() == ColumnType.BOOLEAN) {
    return ((BooleanColumn) c).asDoubleColumn();
  } else if (c.type() == ColumnType.LOCAL_DATE) {
    return ((DateColumn) c).asDoubleColumn();
  } else if (c.type() == ColumnType.LOCAL_DATE_TIME) {
    return ((DateTimeColumn) c).asDoubleColumn();
  } else if (c.type() == ColumnType.INSTANT) {
    return ((InstantColumn) c).asDoubleColumn();
  } else if (c.type() == ColumnType.LOCAL_TIME) {
    return ((TimeColumn) c).asDoubleColumn();
  }
  return (NumericColumn<?>) column(columnIndex);
}
 
Example #3
Source File: RollingColumn.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
public <INCOL extends Column<?>, OUT> Column<?> calc(AggregateFunction<INCOL, OUT> function) {
  // TODO: the subset operation copies the array. creating a view would likely be more efficient
  Column<?> result = function.returnType().create(generateNewColumnName(function));
  for (int i = 0; i < window - 1; i++) {
    result.appendMissing();
  }
  for (int origColIndex = 0; origColIndex < column.size() - window + 1; origColIndex++) {
    Selection selection = new BitmapBackedSelection();
    selection.addRange(origColIndex, origColIndex + window);
    INCOL subsetCol = (INCOL) column.subset(selection.toArray());
    OUT answer = function.summarize(subsetCol);
    if (answer instanceof Number) {
      Number number = (Number) answer;
      ((DoubleColumn) result).append(number.doubleValue());
    } else {
      result.appendObj(answer);
    }
  }
  return result;
}
 
Example #4
Source File: AnalyticQueryEngine.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/**
 * Execute all aggregate functions for the given slice setting values in the appropriate
 * destination column.
 */
private void processAggregateFunctions(TableSlice slice) {
  for (String toColumn : query.getArgumentList().getAggregateFunctions().keySet()) {
    FunctionCall<AggregateFunctions> functionCall =
        query.getArgumentList().getAggregateFunctions().get(toColumn);

    AggregateFunctions aggregateFunction = functionCall.getFunction();
    Column<?> sourceColumn = query.getTable().column(functionCall.getSourceColumnName());
    validateColumn(aggregateFunction, sourceColumn);

    Column<?> destinationColumn = destination.column(functionCall.getDestinationColumnName());
    new WindowSlider(
            query.getWindowFrame(), aggregateFunction, slice, sourceColumn, destinationColumn)
        .execute();
  }
}
 
Example #5
Source File: DataFrameJoiner.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private void withMissingLeftJoin(
    Table destination, Table table1, Selection table1Rows, Set<Integer> ignoreColumns) {
  for (int c = 0; c < destination.columnCount(); c++) {
    if (ignoreColumns.contains(c)) {
      continue;
    }
    if (c < table1.columnCount()) {
      Column t1Col = table1.column(c);
      for (int index : table1Rows) {
        destination.column(c).append(t1Col, index);
      }
    } else {
      for (int r1 = 0; r1 < table1Rows.size(); r1++) {
        destination.column(c).appendMissing();
      }
    }
  }
}
 
Example #6
Source File: Relation.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Returns the columns whose indices are given in the input array */
public List<Column<?>> columns(int... columnIndices) {
  List<Column<?>> cols = new ArrayList<>(columnIndices.length);
  for (int i : columnIndices) {
    cols.add(column(i));
  }
  return cols;
}
 
Example #7
Source File: Table.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public Summarizer summarize(
    Column<?> column1,
    Column<?> column2,
    Column<?> column3,
    Column<?> column4,
    AggregateFunction<?, ?>... function) {
  return new Summarizer(this, column1, column2, column3, column4, function);
}
 
Example #8
Source File: Table.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Removes all columns except for those given in the argument from this table */
public Table retainColumns(Column<?>... columns) {
  List<Column<?>> retained = Arrays.asList(columns);
  columnList.clear();
  columnList.addAll(retained);
  return this;
}
 
Example #9
Source File: AnalyticQueryEngine.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to make sure the given aggregate function is compatible with the type of the source
 * column.
 */
private void validateColumn(FunctionMetaData function, Column<?> sourceColumn) {
  if (!function.isCompatibleColumn(sourceColumn.type())) {
    throw new IllegalArgumentException(
        "Function: "
            + function.functionName()
            + " Is not compatible with column type: "
            + sourceColumn.type());
  }
}
 
Example #10
Source File: TimeColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public TimeColumn append(Column<LocalTime> column) {
  Preconditions.checkArgument(column.type() == this.type());
  TimeColumn timeCol = (TimeColumn) column;
  final int size = timeCol.size();
  for (int i = 0; i < size; i++) {
    appendInternal(timeCol.getIntInternal(i));
  }
  return this;
}
 
Example #11
Source File: SortUtils.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Returns a comparator chain for sorting according to the given key */
public static IntComparatorChain getChain(Table table, Sort key) {
  Iterator<Map.Entry<String, Sort.Order>> entries = key.iterator();
  Map.Entry<String, Sort.Order> sort = entries.next();
  Column<?> column = table.column(sort.getKey());
  IntComparator comparator = rowComparator(column, sort.getValue());

  IntComparatorChain chain = new IntComparatorChain(comparator);
  while (entries.hasNext()) {
    sort = entries.next();
    chain.addComparator(rowComparator(table.column(sort.getKey()), sort.getValue()));
  }
  return chain;
}
 
Example #12
Source File: TraceBuilder.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
protected static String[] columnToStringArray(Column<?> column) {
  String[] x = new String[column.size()];
  for (int i = 0; i < column.size(); i++) {
    x[i] = column.getString(i);
  }
  return x;
}
 
Example #13
Source File: Summarizer.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public Table by(CategoricalColumn<?>... columns) {
  for (Column<?> c : columns) {
    if (!temp.containsColumn(c)) {
      temp.addColumns(c);
    }
  }
  TableSliceGroup group = StandardTableSliceGroup.create(temp, columns);
  return summarize(group);
}
 
Example #14
Source File: Rows.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
public static void appendRowToTable(int row, Table oldTable, Table newTable) {
  int[] rows = new int[] {row};
  for (int columnIndex = 0; columnIndex < oldTable.columnCount(); columnIndex++) {
    Column oldColumn = oldTable.column(columnIndex);
    for (int i : rows) {
      newTable.column(columnIndex).append(oldColumn, i);
    }
  }
}
 
Example #15
Source File: TableSliceGroup.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * 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 (result instanceof Number) {
          Number number = (Number) result;
          resultColumn.append(number.doubleValue());
        } else {
          resultColumn.append(result);
        }
      }
      groupTable.addColumns(resultColumn);
      firstFunction = false;
    }
  }
  return splitGroupingColumn(groupTable);
}
 
Example #16
Source File: HtmlWriter.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private static Element header(List<Column<?>> cols, ElementCreator elements) {
  Element thead = elements.create("thead");
  Element tr = elements.create("tr");
  thead.appendChild(tr);
  for (Column<?> col : cols) {
    tr.appendChild(elements.create("th", col, null).appendText(col.name()));
  }
  return thead;
}
 
Example #17
Source File: XlsxReader.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private Column<?> createColumn(String name, Cell cell) {
  Column<?> column;
  ColumnType columnType = getColumnType(cell);
  if (columnType == null) {
    columnType = ColumnType.STRING;
  }
  column = columnType.create(name);
  return column;
}
 
Example #18
Source File: TableFilteringTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testRejectRows() {
  Table result = table.dropRows(20, 30);
  assertEquals(table.rowCount() - 2, result.rowCount());
  for (Column<?> c : result.columns()) {
    assertEquals(table.getString(21, c.name()), result.getString(20, c.name()));
    assertEquals(table.getString(32, c.name()), result.getString(30, c.name()));
  }
}
 
Example #19
Source File: Table.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public Summarizer summarize(
    Column<?> column1,
    Column<?> column2,
    Column<?> column3,
    AggregateFunction<?, ?>... function) {
  return new Summarizer(this, column1, column2, column3, function);
}
 
Example #20
Source File: Table.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Returns a table with the same columns as this table, but no data */
public Table emptyCopy() {
  Table copy = new Table(name);
  for (Column<?> column : columnList) {
    copy.addColumns(column.emptyCopy());
  }
  return copy;
}
 
Example #21
Source File: Summarizer.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an object capable of summarizing the given columns in the given sourceTable, by
 * applying the given functions
 */
public Summarizer(
    Table sourceTable,
    Column<?> column1,
    Column<?> column2,
    AggregateFunction<?, ?>... functions) {
  Table tempTable = Table.create(sourceTable.name());
  tempTable.addColumns(column1);
  tempTable.addColumns(column2);
  this.temp = tempTable;
  this.original = sourceTable;
  summarizedColumns.add(column1.name());
  summarizedColumns.add(column2.name());
  this.reductions = functions;
}
 
Example #22
Source File: SortUtils.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a comparator that can be used to sort the records in this table according to the given
 * sort key
 */
public static IntComparator getComparator(Table table, Sort key) {
  Iterator<Map.Entry<String, Sort.Order>> entries = key.iterator();
  Map.Entry<String, Sort.Order> sort = entries.next();
  Column<?> column = table.column(sort.getKey());
  return SortUtils.rowComparator(column, sort.getValue());
}
 
Example #23
Source File: FloatColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public FloatColumn append(final Column<Float> column) {
  Preconditions.checkArgument(column.type() == this.type());
  final FloatColumn numberColumn = (FloatColumn) column;
  final int size = numberColumn.size();
  for (int i = 0; i < size; i++) {
    append(numberColumn.getFloat(i));
  }
  return this;
}
 
Example #24
Source File: InstantColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public InstantColumn append(Column<Instant> column) {
  Preconditions.checkArgument(column.type() == this.type());
  InstantColumn dateTimeColumn = (InstantColumn) column;
  final int size = dateTimeColumn.size();
  for (int i = 0; i < size; i++) {
    append(dateTimeColumn.get(i));
  }
  return this;
}
 
Example #25
Source File: XlsxReader.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private Column<?> createColumn(String name, Cell cell) {
  Column<?> column;
  ColumnType columnType = getColumnType(cell);
  if (columnType == null) {
    columnType = ColumnType.STRING;
  }
  column = columnType.create(name);
  return column;
}
 
Example #26
Source File: Table.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new Table initialized with the given names and columns
 *
 * @param name The name of the table
 * @param columns One or more columns, all of which must have either the same length or size 0
 */
protected Table(String name, Collection<Column<?>> columns) {
  this(name);
  for (final Column<?> column : columns) {
    this.addColumns(column);
  }
}
 
Example #27
Source File: StringMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Return a copy of this column with the given string appended
 *
 * @param columns the column to append
 * @return the new column
 */
default StringColumn join(String separator, Column<?>... columns) {
  StringColumn newColumn = StringColumn.create(name() + "[column appended]", this.size());
  for (int r = 0; r < size(); r++) {
    StringBuilder result = new StringBuilder(getString(r));
    for (Column<?> stringColumn : columns) {
      result.append(separator).append(stringColumn.get(r));
    }
    newColumn.set(r, result.toString());
  }
  return newColumn;
}
 
Example #28
Source File: BooleanColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public Column<Boolean> append(Column<Boolean> column, int row) {
  checkArgument(column.type() == this.type());
  BooleanColumn col = (BooleanColumn) column;
  append(col.getByte(row));
  return this;
}
 
Example #29
Source File: Table.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Throws an IllegalArgumentException if the column size doesn't match the rowCount() for the
 * table
 */
private void checkColumnSize(Column<?> newColumn) {
  if (columnCount() != 0) {
    Preconditions.checkArgument(
        newColumn.size() == rowCount(),
        "Column "
            + newColumn.name()
            + " does not have the same number of rows as the other columns in the table.");
  }
}
 
Example #30
Source File: HtmlWriter.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private static Element header(List<Column<?>> cols, ElementCreator elements) {
  Element thead = elements.create("thead");
  Element tr = elements.create("tr");
  thead.appendChild(tr);
  for (Column<?> col : cols) {
    tr.appendChild(elements.create("th", col, null).appendText(col.name()));
  }
  return thead;
}