Java Code Examples for tech.tablesaw.api.IntColumn#create()

The following examples show how to use tech.tablesaw.api.IntColumn#create() . 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 IntColumn assignToGroupsByStep(int step) {
  IntColumn groupColumn = IntColumn.create(GROUP_COL_TEMP_NAME, temp.rowCount());
  temp.addColumns(groupColumn);

  int groupId = 1;
  int withinGroupCount = 0;
  Row row = new Row(temp);

  while (row.hasNext()) {
    row.next();
    if (withinGroupCount < step) {
      withinGroupCount++;
      groupColumn.set(row.getRowNumber(), groupId);
    } else {
      groupId++;
      groupColumn.set(row.getRowNumber(), groupId);
      withinGroupCount = 1;
    }
  }
  int lastGroupSize = temp.where(numberColumn(GROUP_COL_TEMP_NAME).isEqualTo(groupId)).rowCount();
  if (lastGroupSize < step) {
    temp = temp.dropWhere(numberColumn(GROUP_COL_TEMP_NAME).isEqualTo(groupId));
  }
  temp.addColumns(IntColumn.indexColumn("index", temp.rowCount(), 1));
  return groupColumn;
}
 
Example 2
Source File: Summarizer.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private IntColumn assignToGroupsByStep(int step) {
  IntColumn groupColumn = IntColumn.create(GROUP_COL_TEMP_NAME, temp.rowCount());
  temp.addColumns(groupColumn);

  int groupId = 1;
  int withinGroupCount = 0;
  Row row = new Row(temp);

  while (row.hasNext()) {
    row.next();
    if (withinGroupCount < step) {
      withinGroupCount++;
      groupColumn.set(row.getRowNumber(), groupId);
    } else {
      groupId++;
      groupColumn.set(row.getRowNumber(), groupId);
      withinGroupCount = 1;
    }
  }
  int lastGroupSize = temp.where(numberColumn(GROUP_COL_TEMP_NAME).isEqualTo(groupId)).rowCount();
  if (lastGroupSize < step) {
    temp = temp.dropWhere(numberColumn(GROUP_COL_TEMP_NAME).isEqualTo(groupId));
  }
  temp.addColumns(IntColumn.indexColumn("index", temp.rowCount(), 1));
  return groupColumn;
}
 
Example 3
Source File: TimeMapFunctions.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
default IntColumn difference(TimeColumn column2, ChronoUnit unit) {
  IntColumn newColumn =
      IntColumn.create(name() + " - " + column2.name() + "[" + unit.name() + "]");

  for (int r = 0; r < size(); r++) {
    int c1 = this.getIntInternal(r);
    int c2 = column2.getIntInternal(r);
    if (TimeColumn.valueIsMissing(c1) || TimeColumn.valueIsMissing(c2)) {
      newColumn.append(IntColumnType.missingValueIndicator());
    } else {
      LocalTime value1 = PackedLocalTime.asLocalTime(c1);
      LocalTime value2 = PackedLocalTime.asLocalTime(c2);
      if (value1 != null && value2 != null) {
        newColumn.append((int) unit.between(value1, value2));
      } else {
        newColumn.appendMissing();
      }
    }
  }
  return newColumn;
}
 
Example 4
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a column containing integers representing the nth group (0-based) that a date falls
 * into.
 *
 * <p>Example: When Unit = ChronoUnit.DAY and n = 5, we form 5 day groups. a Date that is 2 days
 * after the start is assigned to the first ("0") group. A day 7 days after the start is assigned
 * to the second ("1") group.
 *
 * @param unit A ChronoUnit greater than or equal to a day
 * @param n The number of units in each group.
 * @param start The starting point of the first group; group boundaries are offsets from this
 *     point
 */
default IntColumn timeWindow(ChronoUnit unit, int n, LocalDate start) {
  String newColumnName = "" + n + " " + unit.toString() + " window [" + name() + "]";
  int packedStartDate = PackedLocalDate.pack(start);
  IntColumn numberColumn = IntColumn.create(newColumnName, size());
  for (int i = 0; i < size(); i++) {
    int packedDate = getIntInternal(i);
    int result;
    switch (unit) {
      case DAYS:
        result = PackedLocalDate.daysUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      case WEEKS:
        result = PackedLocalDate.weeksUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      case MONTHS:
        result = PackedLocalDate.monthsUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      case YEARS:
        result = PackedLocalDate.yearsUntil(packedDate, packedStartDate) / n;
        numberColumn.set(i, result);
        break;
      default:
        throw new UnsupportedTemporalTypeException(
            "The ChronoUnit " + unit + " is not supported for timeWindows on dates");
    }
  }
  numberColumn.setPrintFormatter(NumberColumnFormatter.ints());
  return numberColumn;
}
 
Example 5
Source File: TimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn milliseconds() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "ms" + "]");
  for (int r = 0; r < size(); r++) {
    int c1 = getIntInternal(r);
    if (!TimeColumn.valueIsMissing(c1)) {
      newColumn.append(PackedLocalTime.getMilliseconds(c1));
    } else {
      newColumn.appendMissing();
    }
  }
  return newColumn;
}
 
Example 6
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn minuteOfDay() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "minute-of-day" + "]");
  for (int r = 0; r < size(); r++) {
    if (!isMissing(r)) {
      long c1 = getLongInternal(r);
      newColumn.append((short) getMinuteOfDay(c1));
    } else {
      newColumn.appendMissing();
    }
  }
  return newColumn;
}
 
Example 7
Source File: TimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn minuteOfDay() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "minute-of-day" + "]");
  for (int r = 0; r < size(); r++) {
    int c1 = getIntInternal(r);
    if (!TimeColumn.valueIsMissing(c1)) {
      newColumn.append(PackedLocalTime.getMinuteOfDay(c1));
    } else {
      newColumn.appendMissing();
    }
  }
  return newColumn;
}
 
Example 8
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn hour() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "hour" + "]");
  for (int r = 0; r < size(); r++) {
    if (!isMissing(r)) {
      long c1 = getLongInternal(r);
      newColumn.append(getHour(c1));
    } else {
      newColumn.appendMissing();
    }
  }
  return newColumn;
}
 
Example 9
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfYear() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of year", this.size());
  for (int r = 0; r < this.size(); r++) {
    if (!isMissing(r)) {
      long c1 = this.getLongInternal(r);
      newColumn.set(r, (short) getDayOfYear(c1));
    }
  }
  return newColumn;
}
 
Example 10
Source File: DataFrameJoinerPerformanceTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private static Table addFillerColumn(Table table, int numberColumnsToAdd, String prefix) {
  int[] filler = new int[table.rowCount()];
  Arrays.fill(filler, 1);
  IntColumn col = IntColumn.create("temp", filler);
  for (int i = 0; i < numberColumnsToAdd; i++) {
    table.addColumns(col.copy().setName(prefix + "_appendColumn" + i));
  }
  return table;
}
 
Example 11
Source File: TimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn minuteOfDay() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "minute-of-day" + "]");
  for (int r = 0; r < size(); r++) {
    int c1 = getIntInternal(r);
    if (!TimeColumn.valueIsMissing(c1)) {
      newColumn.append(PackedLocalTime.getMinuteOfDay(c1));
    } else {
      newColumn.appendMissing();
    }
  }
  return newColumn;
}
 
Example 12
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn hour() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "hour" + "]");
  for (int r = 0; r < size(); r++) {
    if (!isMissing(r)) {
      long c1 = getLongInternal(r);
      newColumn.append(getHour(c1));
    } else {
      newColumn.appendMissing();
    }
  }
  return newColumn;
}
 
Example 13
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the temporal difference between each element of the receiver and the respective
 * element of the argument
 *
 * <p>Missing values in either result in a Missing Value for the new column
 */
default IntColumn timeUntil(DateColumn end, ChronoUnit unit) {

  IntColumn newColumn = IntColumn.create(name() + " - " + end.name() + "[" + unit.name() + "]");

  for (int r = 0; r < size(); r++) {
    int c1 = getIntInternal(r);
    int c2 = end.getIntInternal(r);
    if (valueIsMissing(c1) || valueIsMissing(c2)) {
      newColumn.appendMissing();
    } else {
      switch (unit) {
        case DAYS:
          newColumn.append(PackedLocalDate.daysUntil(c2, c1));
          break;
        case WEEKS:
          newColumn.append(PackedLocalDate.weeksUntil(c2, c1));
          break;
        case MONTHS:
          newColumn.append(PackedLocalDate.monthsUntil(c2, c1));
          break;
        case YEARS:
          newColumn.append(PackedLocalDate.yearsUntil(c2, c1));
          break;
        default: // handle decades, etc.
          LocalDate value1 = PackedLocalDate.asLocalDate(c1);
          LocalDate value2 = PackedLocalDate.asLocalDate(c2);
          if (value1 == null || value2 == null) {
            newColumn.appendMissing();
          } else {
            newColumn.append((int) unit.between(value1, value2));
          }
          break;
      }
    }
  }
  return newColumn;
}
 
Example 14
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn dayOfWeekValue() {
  IntColumn newColumn = IntColumn.create(this.name() + " day of week value", this.size());
  for (int r = 0; r < this.size(); r++) {
    if (!isMissing(r)) {
      long c1 = this.getLongInternal(r);
      newColumn.set(r, (short) getDayOfWeek(c1).getValue());
    }
  }
  return newColumn;
}
 
Example 15
Source File: TimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn secondOfDay() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "second-of-day" + "]");
  for (int r = 0; r < size(); r++) {
    int c1 = getIntInternal(r);
    if (!TimeColumn.valueIsMissing(c1)) {
      newColumn.append(PackedLocalTime.getSecondOfDay(c1));
    } else {
      newColumn.append(IntColumnType.missingValueIndicator());
    }
  }
  return newColumn;
}
 
Example 16
Source File: TimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default IntColumn minute() {
  IntColumn newColumn = IntColumn.create(name() + "[" + "minute" + "]");
  for (int r = 0; r < size(); r++) {
    int c1 = getIntInternal(r);
    if (!IntColumn.valueIsMissing(c1)) {
      newColumn.append(PackedLocalTime.getMinute(c1));
    } else {
      newColumn.append(IntColumnType.missingValueIndicator());
    }
  }
  return newColumn;
}
 
Example 17
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the temporal difference between each element of the receiver and the respective
 * element of the argument
 *
 * <p>Missing values in either result in a Missing Value for the new column
 */
default IntColumn timeUntil(DateColumn end, ChronoUnit unit) {

  IntColumn newColumn = IntColumn.create(name() + " - " + end.name() + "[" + unit.name() + "]");

  for (int r = 0; r < size(); r++) {
    int c1 = getIntInternal(r);
    int c2 = end.getIntInternal(r);
    if (valueIsMissing(c1) || valueIsMissing(c2)) {
      newColumn.appendMissing();
    } else {
      switch (unit) {
        case DAYS:
          newColumn.append(PackedLocalDate.daysUntil(c2, c1));
          break;
        case WEEKS:
          newColumn.append(PackedLocalDate.weeksUntil(c2, c1));
          break;
        case MONTHS:
          newColumn.append(PackedLocalDate.monthsUntil(c2, c1));
          break;
        case YEARS:
          newColumn.append(PackedLocalDate.yearsUntil(c2, c1));
          break;
        default: // handle decades, etc.
          LocalDate value1 = PackedLocalDate.asLocalDate(c1);
          LocalDate value2 = PackedLocalDate.asLocalDate(c2);
          if (value1 == null || value2 == null) {
            newColumn.appendMissing();
          } else {
            newColumn.append((int) unit.between(value1, value2));
          }
          break;
      }
    }
  }
  return newColumn;
}
 
Example 18
Source File: DataFrameJoinerPerformanceTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private static Table addFillerColumn(Table table, int numberColumnsToAdd, String prefix) {
  int[] filler = new int[table.rowCount()];
  Arrays.fill(filler, 1);
  IntColumn col = IntColumn.create("temp", filler);
  for (int i = 0; i < numberColumnsToAdd; i++) {
    table.addColumns(col.copy().setName(prefix + "_appendColumn" + i));
  }
  return table;
}
 
Example 19
Source File: TableSliceTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void iterateOverRowsWithSelection() {
  IntColumn rowNumbers =
      IntColumn.create("originalRowNumber", IntStream.range(0, source.rowCount()).toArray());
  source.addColumns(rowNumbers);

  TableSlice tableSlice = new TableSlice(source, Selection.with(3, 4));

  int count = 0;
  for (Row row : tableSlice) {
    assertEquals(count + 3, row.getInt(3));
    count++;
  }
  assertEquals(2, count);
}
 
Example 20
Source File: HoverBroadcastExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  int seriesLen = 50;
  Random rng = new Random();
  Double[] prices = new Double[seriesLen];
  int[] volumes = new int[seriesLen];
  LocalDateTime startDT = LocalDateTime.of(2019, 1, 1, 9, 30, 0);
  LocalDateTime[] times = new LocalDateTime[seriesLen];

  HoverBroadcastBody hbb =
      HoverBroadcastBody.builder().subPlots(new String[] {"xy", "xy2"}).numTraces(2).build();

  HoverEventHandler heh = HoverEventHandler.builder().body(hbb).build();

  for (int i = 0; i < seriesLen; i++) {
    prices[i] = 25.0 + rng.nextDouble();
    volumes[i] = rng.nextInt(10000);
    times[i] = startDT.plusMinutes(i);
  }

  DateTimeColumn x = DateTimeColumn.create("time", times);
  DoubleColumn y2 = DoubleColumn.create("price", prices);
  IntColumn y1 = IntColumn.create("volume", volumes);

  ScatterTrace trace0 =
      ScatterTrace.builder(x, y2)
          .showLegend(true)
          .name("Price")
          .mode(ScatterTrace.Mode.LINE)
          .yAxis(ScatterTrace.YAxis.Y2)
          .build();

  ScatterTrace trace1 =
      ScatterTrace.builder(x, y1)
          .showLegend(true)
          .name("Volume")
          .mode(ScatterTrace.Mode.LINE)
          .yAxis(ScatterTrace.YAxis.Y)
          .build();

  Layout layout =
      Layout.builder("Stock Data", "Time", "Price")
          .yAxis(Axis.builder().title("volume").domain(0f, 0.25f).build())
          .yAxis2(Axis.builder().title("price").domain(0.35f, 1.0f).build())
          .build();

  Plot.show(
      new FigureBuilder().layout(layout).addEventHandlers(heh).addTraces(trace0, trace1).build());
}