tech.tablesaw.api.IntColumn Java Examples
The following examples show how to use
tech.tablesaw.api.IntColumn.
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: SawReader.java From tablesaw with Apache License 2.0 | 6 votes |
private static IntColumn readIntColumn(String fileName, ColumnMetadata metadata) throws IOException { IntColumn ints = IntColumn.create(metadata.getName()); try (FileInputStream fis = new FileInputStream(fileName); SnappyFramedInputStream sis = new SnappyFramedInputStream(fis, true); DataInputStream dis = new DataInputStream(sis)) { boolean EOF = false; while (!EOF) { try { ints.append(dis.readInt()); } catch (EOFException e) { EOF = true; } } } return ints; }
Example #2
Source File: MarkerOptionsExample.java From tablesaw with Apache License 2.0 | 6 votes |
/** * Shows a scatter with color set as a color scale * * <p>The color scale requires that an array of numeric values be provided, here we just scale * according to the number of wins the team has. */ private void showColorScale() { Layout layout = Layout.builder() .title("color scaled by # of wins") .xAxis(Axis.builder().title("Batting Average").build()) .yAxis(Axis.builder().title("Wins").build()) .build(); IntColumn wins = baseball.intColumn("W"); Trace trace = ScatterTrace.builder(x, y) .marker( Marker.builder() .color(wins.asDoubleArray()) .cMinAndMax(wins.min(), wins.max()) .colorScale(Marker.Palette.YL_GN_BU) .build()) .build(); Plot.show(new Figure(layout, trace)); }
Example #3
Source File: HistogramFuncExample.java From tablesaw with Apache License 2.0 | 6 votes |
public static void main(String[] args) { Table test = Table.create( StringColumn.create("type").append("apples").append("apples").append("apples").append("oranges").append("bananas"), IntColumn.create("num").append(5).append(10).append(3).append(10).append(5)); Layout layout1 = Layout.builder().title("Histogram COUNT Test (team batting averages)").build(); HistogramTrace trace = HistogramTrace. builder(test.stringColumn("type"), test.intColumn("num")) .histFunc(COUNT) .build(); Plot.show(new Figure(layout1, trace)); Layout layout2 = Layout.builder().title("Hist SUM Test (team batting averages)").build(); HistogramTrace trace2 = HistogramTrace. builder(test.stringColumn("type"), test.intColumn("num")) .histFunc(SUM) .build(); Plot.show(new Figure(layout2, trace2)); }
Example #4
Source File: MarkerOptionsExample.java From tablesaw with Apache License 2.0 | 6 votes |
/** * Shows a scatter with color set as a color scale * * <p>The color scale requires that an array of numeric values be provided, here we just scale * according to the number of wins the team has. */ private void showColorScaleWithBar() { Layout layout = Layout.builder() .title("color scaled with color bar") .xAxis(Axis.builder().title("Batting Average").build()) .yAxis(Axis.builder().title("Wins").build()) .build(); IntColumn wins = baseball.intColumn("W"); Trace trace = ScatterTrace.builder(x, y) .marker( Marker.builder() .color(wins.asDoubleArray()) .cMinAndMax(wins.min(), wins.max()) .colorScale(Marker.Palette.YL_GN_BU) .showScale(true) .build()) .build(); Plot.show(new Figure(layout, trace)); }
Example #5
Source File: MarkerOptionsExample.java From tablesaw with Apache License 2.0 | 6 votes |
/** * Shows a scatter with color set as a color scale * * <p>The color scale requires that an array of numeric values be provided, here we just scale * according to the number of wins the team has. */ private void showColorScale() { Layout layout = Layout.builder() .title("color scaled by # of wins") .xAxis(Axis.builder().title("Batting Average").build()) .yAxis(Axis.builder().title("Wins").build()) .build(); IntColumn wins = baseball.intColumn("W"); Trace trace = ScatterTrace.builder(x, y) .marker( Marker.builder() .color(wins.asDoubleArray()) .cMinAndMax(wins.min(), wins.max()) .colorScale(Marker.Palette.YL_GN_BU) .build()) .build(); Plot.show(new Figure(layout, trace)); }
Example #6
Source File: NumberMapFunctionsTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void lag() { IntColumn n1 = IntColumn.indexColumn("index", 4, 0); Table t = Table.create("tst"); t.addColumns(n1, n1.lag(-2)); assertEquals( " tst " + LINE_END + " index | index lag(-2) |" + LINE_END + "---------------------------" + LINE_END + " 0 | 2 |" + LINE_END + " 1 | 3 |" + LINE_END + " 2 | |" + LINE_END + " 3 | |", t.print()); }
Example #7
Source File: Summarizer.java From tablesaw with Apache License 2.0 | 6 votes |
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 #8
Source File: IntIndex.java From tablesaw with Apache License 2.0 | 6 votes |
public IntIndex(IntColumn column) { Preconditions.checkArgument( column.type().equals(ColumnType.INTEGER), "Int indexing only allowed on INTEGER numeric columns"); int sizeEstimate = Integer.min(1_000_000, column.size() / 100); Int2ObjectOpenHashMap<IntArrayList> tempMap = new Int2ObjectOpenHashMap<>(sizeEstimate); for (int i = 0; i < column.size(); i++) { int value = column.getInt(i); IntArrayList recordIds = tempMap.get(value); if (recordIds == null) { recordIds = new IntArrayList(); recordIds.add(i); tempMap.trim(); tempMap.put(value, recordIds); } else { recordIds.add(i); } } index = new Int2ObjectAVLTreeMap<>(tempMap); }
Example #9
Source File: ColumnTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void testSetMissingTo() { Double[] d1 = {1d, null, -1d}; Integer[] i1 = {2, null, 3}; String[] s1 = {"a", null, "C"}; LocalDate[] dt1 = {LocalDate.now(), null, LocalDate.now()}; DoubleColumn dc1 = DoubleColumn.create("t1", d1); IntColumn ic1 = IntColumn.create("t2", i1); StringColumn sc1 = StringColumn.create("t3", s1); DateColumn dtc1 = DateColumn.create("t4", dt1); dc1.setMissingTo(-34.2); assertTrue(dc1.contains(-34.2)); ic1.setMissingTo(-34); assertTrue(ic1.contains(-34)); sc1.setMissingTo("missing"); assertTrue(sc1.contains("missing")); dtc1.setMissingTo(LocalDate.of(2001, 1, 1)); assertTrue(dtc1.contains(LocalDate.of(2001, 1, 1))); }
Example #10
Source File: AnalyticQueryTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void testToSqlString() { Table table = Table.create("table1", IntColumn.create("sales")); AnalyticQuery query = AnalyticQuery.query() .from(table) .partitionBy("product", "region") .orderBy("sales") .rowsBetween() .unboundedPreceding() .andUnBoundedFollowing() .sum("sales") .as("sumSales") .build(); String expected = "SELECT" + System.lineSeparator() + "SUM(sales) OVER w1 AS sumSales" + System.lineSeparator() + "FROM table1" + System.lineSeparator() + "Window w1 AS (" + System.lineSeparator() + "PARTITION BY product, region" + System.lineSeparator() + "ORDER BY sales ASC" + System.lineSeparator() + "ROWS BETWEEN UNBOUNDED_PRECEDING AND UNBOUNDED_FOLLOWING);"; assertEquals(expected, query.toSqlLikeString()); }
Example #11
Source File: TimeMapFunctions.java From tablesaw with Apache License 2.0 | 6 votes |
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 #12
Source File: CrossTabTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void testCounts3() throws Exception { Table bush = Table.read().csv("../data/bush.csv"); IntColumn month = bush.dateColumn("date").monthValue(); month.setName("month"); BooleanColumn seventyPlus = BooleanColumn.create( "70", bush.numberColumn("approval").isGreaterThanOrEqualTo(70), bush.rowCount()); seventyPlus.setName("seventyPlus"); bush.addColumns(month, seventyPlus); Table counts = bush.xTabCounts("month", "seventyPlus"); for (Row row : counts) { assertEquals( counts.intColumn("total").get(row.getRowNumber()), row.getInt("true") + row.getInt("false"), 0.01); } assertTrue(counts.numberColumn("[labels]").isMissing(counts.rowCount() - 1)); }
Example #13
Source File: TimeMapFunctions.java From tablesaw with Apache License 2.0 | 6 votes |
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 #14
Source File: JsonReaderTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void arrayOfRowsWithIncompleteIndexes() { String json = "[" + "{\"A\" : \"123\", \"B\" : \"456\"}," + "{\"B\" : \"789\", \"C\" : \"123\"}" + "]"; Table expected = Table.create( IntColumn.create("A", new int[] {123, Integer.MIN_VALUE}), IntColumn.create("B", new int[] {456, 789}), IntColumn.create("C", new int[] {Integer.MIN_VALUE, 123})); Table actual = Table.read().string(json, "json"); assertEquals(ColumnType.INTEGER, actual.columnTypes()[0]); assertEquals(expected.column("A").asList(), actual.column("A").asList()); assertEquals(expected.column("B").asList(), actual.column("B").asList()); assertEquals(expected.column("C").asList(), actual.column("C").asList()); }
Example #15
Source File: MarkerOptionsExample.java From tablesaw with Apache License 2.0 | 6 votes |
/** * Shows a scatter with color set as a color scale * * <p>The color scale requires that an array of numeric values be provided, here we just scale * according to the number of wins the team has. */ private void showColorScaleWithBar() { Layout layout = Layout.builder() .title("color scaled with color bar") .xAxis(Axis.builder().title("Batting Average").build()) .yAxis(Axis.builder().title("Wins").build()) .build(); IntColumn wins = baseball.intColumn("W"); Trace trace = ScatterTrace.builder(x, y) .marker( Marker.builder() .color(wins.asDoubleArray()) .cMinAndMax(wins.min(), wins.max()) .colorScale(Marker.Palette.YL_GN_BU) .showScale(true) .build()) .build(); Plot.show(new Figure(layout, trace)); }
Example #16
Source File: AnalyticQueryTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void toSqlStringNumbering() { AnalyticQuery query = AnalyticQuery.numberingQuery() .from(Table.create("myTable", IntColumn.create("date"), IntColumn.create("region"))) .partitionBy() .orderBy("date", "region") .rank() .as("myRank") .build(); String expectd = "SELECT\n" + "RANK() OVER w1 AS myRank\n" + "FROM myTable\n" + "Window w1 AS (\n" + "ORDER BY date ASC, region ASC);"; assertEquals(expectd, query.toSqlLikeString()); }
Example #17
Source File: DateTimeMapFunctionsTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void testSecondOfDay() { LocalDateTime dateTime = LocalDateTime.of(2018, 4, 10, 7, 30); startCol.append(dateTime); IntColumn secondOfDay = startCol.secondOfDay(); assertEquals(dateTime.get(ChronoField.SECOND_OF_DAY), secondOfDay.get(0), 0.0001); }
Example #18
Source File: DateTimeMapFunctionsTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void testMinuteOfDay() { LocalDateTime dateTime = LocalDateTime.of(2018, 4, 10, 7, 30); startCol.append(dateTime); IntColumn minuteOfDay = startCol.minuteOfDay(); assertEquals((7 * 60) + 30, minuteOfDay.get(0), 0.0001); }
Example #19
Source File: DateMapFunctions.java From tablesaw with Apache License 2.0 | 5 votes |
/** * 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 #20
Source File: DateTimeMapFunctions.java From tablesaw with Apache License 2.0 | 5 votes |
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 #21
Source File: DateTimeMapFunctions.java From tablesaw with Apache License 2.0 | 5 votes |
default IntColumn dayOfMonth() { IntColumn newColumn = IntColumn.create(this.name() + " day of month", size()); for (int r = 0; r < this.size(); r++) { if (!isMissing(r)) { long c1 = this.getLongInternal(r); newColumn.set(r, getDayOfMonth(c1)); } } return newColumn; }
Example #22
Source File: TableSliceTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void removeSort() { Selection selection = Selection.withRange(0, 5); TableSlice tableSlice = new TableSlice(source, selection); tableSlice.sortOn(Sort.on("approval", Order.ASCEND)); tableSlice.removeSort(); double[] expected = new double[] {53.0, 53.0, 58.0, 52.0, 52.0}; double[] actual = ((IntColumn) tableSlice.column("approval")).asDoubleArray(); assertArrayEquals(expected, actual); }
Example #23
Source File: DateMapFunctions.java From tablesaw with Apache License 2.0 | 5 votes |
/** * 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 #24
Source File: ColumnMetadata.java From tablesaw with Apache License 2.0 | 5 votes |
public Column<?> createColumn() { final String typeString = getType(); switch (typeString) { case FLOAT: return FloatColumn.create(name); case DOUBLE: return DoubleColumn.create(name); case INTEGER: return IntColumn.create(name); case BOOLEAN: return BooleanColumn.create(name); case LOCAL_DATE: return DateColumn.create(name); case LOCAL_TIME: return TimeColumn.create(name); case LOCAL_DATE_TIME: return DateTimeColumn.create(name); case INSTANT: return DateTimeColumn.create(name); case STRING: return StringColumn.create(name); case TEXT: return TextColumn.create(name); case SHORT: return ShortColumn.create(name); case LONG: return LongColumn.create(name); default: throw new IllegalStateException("Unhandled column type writing columns: " + typeString); } }
Example #25
Source File: DateTimeMapFunctions.java From tablesaw with Apache License 2.0 | 5 votes |
default IntColumn secondOfDay() { IntColumn newColumn = IntColumn.create(name() + "[" + "second-of-day" + "]"); for (int r = 0; r < size(); r++) { if (!isMissing(r)) { long c1 = getLongInternal(r); newColumn.append(getSecondOfDay(c1)); } else { newColumn.appendMissing(); } } return newColumn; }
Example #26
Source File: TableTransposeTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test void transposeIntegers() { Table testTable = Table.create( TABLE_NAME, IntColumn.create("value1", 1, 2, 3), IntColumn.create("value2", 4, 5, 6)); Table result = testTable.transpose(); assertTableEquals( Table.create( TABLE_NAME, IntColumn.create("0", 1, 4), IntColumn.create("1", 2, 5), IntColumn.create("2", 3, 6)), result); }
Example #27
Source File: OutcomeTable.java From Dhalion with MIT License | 5 votes |
OutcomeTable(String name) { id = new IntColumn(ID); assignment = new CategoryColumn(ASSIGNMENT); type = new CategoryColumn(TYPE); timeStamp = new LongColumn(TIME_STAMP); table = Table.create(name); table.addColumn(id); table.addColumn(assignment); table.addColumn(type); table.addColumn(timeStamp); }
Example #28
Source File: ByteDictionaryMap.java From tablesaw with Apache License 2.0 | 5 votes |
/** */ @Override public Table countByCategory(String columnName) { Table t = Table.create("Column: " + columnName); StringColumn categories = StringColumn.create("Category"); IntColumn counts = IntColumn.create("Count"); // Now uses the keyToCount map for (Map.Entry<Byte, Integer> entry : keyToCount.byte2IntEntrySet()) { categories.append(getValueForKey(entry.getKey())); counts.append(entry.getValue()); } t.addColumns(categories); t.addColumns(counts); return t; }
Example #29
Source File: DataFrameJoinerPerformanceTest.java From tablesaw with Apache License 2.0 | 5 votes |
private static Table createCustomersTable(int numberCustomers) { Table customersTable = Table.create("customers"); IntColumn customerIds = IntColumn.create("customerId", IntStream.range(0, numberCustomers).toArray()); customersTable.addColumns(customerIds); return customersTable; }
Example #30
Source File: DateTimeMapFunctions.java From tablesaw with Apache License 2.0 | 5 votes |
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; }