Java Code Examples for tech.tablesaw.api.Table#create()
The following examples show how to use
tech.tablesaw.api.Table#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: AnalyticQueryTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void executeInPlaceNumbering() { Table table = Table.create("table", StringColumn.create("col1", new String[] {})); AnalyticQuery.numberingQuery() .from(table) .partitionBy() .orderBy("col1") .rowNumber() .as("rowNumber") .rank() .as("rank") .denseRank() .as("denseRank") .executeInPlace(); assertEquals(ImmutableList.of("col1", "rowNumber", "rank", "denseRank"), table.columnNames()); }
Example 2
Source File: TableTransposeTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test void transposeUseFirstColumnForHeadings() { Table testTable = Table.create( TABLE_NAME, StringColumn.create("label", "row1", "row2", "row3"), DoubleColumn.create("value1", 1.0, 1.1, 1.2), DoubleColumn.create("value2", 2.0, 2.1, 2.2)); Table result = testTable.transpose(false, true); assertTableEquals( Table.create( TABLE_NAME, DoubleColumn.create("row1", 1.0, 2.0), DoubleColumn.create("row2", 1.1, 2.1), DoubleColumn.create("row3", 1.2, 2.2)), result); }
Example 3
Source File: AnalyticQueryEngineTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void countWithStrings() { Table table = Table.create( "table", StringColumn.create("col1", new String[] {"A", "B", null, "C", "C", "C", "D"})); AnalyticQuery query = AnalyticQuery.quickQuery() .from(table) .rowsBetween() .unboundedPreceding() .andCurrentRow() .count("col1") .as("count") .build(); AnalyticQueryEngine queryEngine = AnalyticQueryEngine.create(query); Table result = queryEngine.execute(); assertEquals(ImmutableList.of(1, 2, 2, 3, 4, 5, 6), result.intColumn("count").asList()); }
Example 4
Source File: AnalyticQueryTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void executeInPlaceNumbering() { Table table = Table.create("table", StringColumn.create("col1", new String[] {})); AnalyticQuery.numberingQuery() .from(table) .partitionBy() .orderBy("col1") .rowNumber() .as("rowNumber") .rank() .as("rank") .denseRank() .as("denseRank") .executeInPlace(); assertEquals(ImmutableList.of("col1", "rowNumber", "rank", "denseRank"), table.columnNames()); }
Example 5
Source File: Summarizer.java From tablesaw with Apache License 2.0 | 6 votes |
/** * 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, Column<?> column3, Column<?> column4, AggregateFunction<?, ?>... functions) { Preconditions.checkArgument(!sourceTable.isEmpty(), "The table to summarize is empty."); Table tempTable = Table.create(sourceTable.name()); tempTable.addColumns(column1); tempTable.addColumns(column2); tempTable.addColumns(column3); tempTable.addColumns(column4); this.temp = tempTable; this.original = sourceTable; summarizedColumns.add(column1.name()); summarizedColumns.add(column2.name()); summarizedColumns.add(column3.name()); summarizedColumns.add(column4.name()); this.reductions = functions; }
Example 6
Source File: AnalyticQueryTest.java From tablesaw with Apache License 2.0 | 6 votes |
@Test public void executeInPlaceWithDuplicateColumnsThrows() { Table table = Table.create("myTable", DoubleColumn.create("col1", new Double[] {})); Throwable thrown = assertThrows( IllegalArgumentException.class, () -> AnalyticQuery.query() .from(table) .partitionBy() .orderBy("col1") .rowsBetween() .unboundedPreceding() .andUnBoundedFollowing() .sum("col1") .as("col1") .executeInPlace()); assertTrue(thrown.getMessage().contains("Cannot add column with duplicate name")); }
Example 7
Source File: SortTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void createSortInvalidPrefixColumnExists() { Table table = Table.create("t", DoubleColumn.create("col1")); Throwable thrown = assertThrows(IllegalStateException.class, () -> Sort.create(table, "<col1")); assertEquals("Column prefix: < is unknown.", thrown.getMessage()); }
Example 8
Source File: DataFramePrinterTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void printWithSmallFloatNumber() { FloatColumn col = FloatColumn.create("testCol"); col.append(0.000003f); Table table = Table.create("small float table", col); String out = table.print(); assertTrue(out.contains("0.000003")); }
Example 9
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 10
Source File: DataFrameReaderTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void csv() throws IOException { Path path = mockFileHelper("/data/file.csv", ImmutableList.of("region", "canada", "us")); Table expected = Table.create(StringColumn.create("region", new String[] {"canada", "us"})); Table actual = Table.read().csv(Files.newInputStream(path)); assertEquals(expected.columnNames(), actual.columnNames()); assertEquals(expected.stringColumn(0).asList(), actual.stringColumn(0).asList()); }
Example 11
Source File: GettingStarted.java From tablesaw with Apache License 2.0 | 5 votes |
private void summarizing() { Table table = Table.create("table"); try { // @@ summarize_basic Table summary = table.summarize("sales", mean, sum, min, max).by("province", "status"); // @@ summarize_basic // @@ summarize_calculated_column summary = table.summarize("sales", mean, median) .by(table.dateColumn("sales date").dayOfWeek()); // @@ summarize_calculated_column } catch (Exception e){} }
Example 12
Source File: LocalTimeFilterTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void testAfter() { Table t = Table.create("test"); t.addColumns(column1); column1.appendCell("05:15:30"); column1.appendCell("10:15:30"); Table result = t.where(t.timeColumn("Game time").isAfter(LocalTime.of(7, 4, 2, 0))); assertEquals(1, result.rowCount()); }
Example 13
Source File: TableSummaryTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void summaryMixedTypes() { Table testTable = Table.create( "Data", StringColumn.create("label", "yellow", "yellow", "green"), DoubleColumn.create("value1", 1.0, 1.1, 1.2), BooleanColumn.create("truthy", true, false, true), DateColumn.create( "dates", new LocalDate[] { LocalDate.of(2001, 1, 1), LocalDate.of(2002, 1, 1), LocalDate.of(2001, 1, 1) })); Table result = testTable.summary(); assertEquals( " Data \n" + " Summary | label | value1 | truthy | dates |\n" + "---------------------------------------------------------------------------\n" + " Count | 3 | 3 | | 3 |\n" + " Unique | 2 | | | |\n" + " Top | yellow | | | |\n" + " Top Freq. | 2 | | | |\n" + " sum | | 3.3 | | |\n" + " Mean | | 1.1 | | |\n" + " Min | | 1 | | |\n" + " Max | | 1.2 | | |\n" + " Range | | 0.19999999999999996 | | |\n" + " Variance | | 0.009999999999999995 | | |\n" + " Std. Dev | | 0.09999999999999998 | | |\n" + " false | | | 1 | |\n" + " true | | | 2 | |\n" + " Missing | | | | 0 |\n" + " Earliest | | | | 2001-01-01 |\n" + " Latest | | | | 2002-01-01 |", result.print()); }
Example 14
Source File: Summarizer.java From tablesaw with Apache License 2.0 | 5 votes |
/** * Returns an object capable of summarizing the given column in the given sourceTable, by applying * the given functions */ public Summarizer(Table sourceTable, Column<?> column, AggregateFunction<?, ?>... functions) { Table tempTable = Table.create(sourceTable.name()); tempTable.addColumns(column); this.temp = tempTable; this.original = sourceTable; summarizedColumns.add(column.name()); this.reductions = functions; }
Example 15
Source File: CsvWriterTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void toWriterWithExtension() throws IOException { StringColumn colA = StringColumn.create("colA", ImmutableList.of("a", "b")); StringColumn colB = StringColumn.create("colB", ImmutableList.of("1", "2")); Table table = Table.create("testTable", colA, colB); StringWriter writer = new StringWriter(); table.write().toWriter(writer, "csv"); assertEquals("colA,colB\na,1\nb,2\n", writer.toString().replaceAll("\\r\\n", "\n")); }
Example 16
Source File: DataFramePrinterTest.java From tablesaw with Apache License 2.0 | 5 votes |
@Test public void printWithSmallFloatNumber() { FloatColumn col = FloatColumn.create("testCol"); col.append(0.000003f); Table table = Table.create("small float table", col); String out = table.print(); assertTrue(out.contains("0.000003")); }
Example 17
Source File: TableSliceGroup.java From tablesaw with Apache License 2.0 | 4 votes |
public static Table summaryTableName(Table source) { return Table.create(source.name() + " summary"); }
Example 18
Source File: DateFiltersTest.java From tablesaw with Apache License 2.0 | 4 votes |
@Test public void testColumnComparisons() { LocalDate dateTime = LocalDate.of(2015, 1, 25); DateColumn dateColumn = DateColumn.create("test"); LocalDate beforeDate = dateTime.minusDays(1); LocalDate afterDate = dateTime.plusDays(1); dateColumn.append(beforeDate); dateColumn.append(dateTime); dateColumn.append(afterDate); DateColumn same = DateColumn.create("same"); same.append(beforeDate); same.append(dateTime); same.append(afterDate); DateColumn before = DateColumn.create("before"); before.append(beforeDate.minusDays(1)); before.append(dateTime.minusDays(1)); before.append(afterDate.minusDays(1)); DateColumn after = DateColumn.create("after"); after.append(beforeDate.plusDays(1)); after.append(dateTime.plusDays(1)); after.append(afterDate.plusDays(1)); Table t = Table.create("test", dateColumn, same, before, after); assertTrue(dateColumn.isOnOrAfter(same).contains(0)); assertTrue(t.dateColumn("test").isOnOrAfter(same).contains(0)); assertTrue(t.dateColumn("test").isOnOrAfter(t.dateColumn("same")).contains(0)); assertTrue(dateColumn.isOnOrBefore(same).contains(0)); assertTrue(t.dateColumn("test").isOnOrBefore(same).contains(0)); assertTrue(t.dateColumn("test").isOnOrBefore(t.dateColumn("same")).contains(0)); assertTrue(dateColumn.isEqualTo(same).contains(0)); assertTrue(t.dateColumn("test").isEqualTo(same).contains(0)); assertTrue(t.dateColumn("test").isEqualTo(t.dateColumn("same")).contains(0)); assertTrue(dateColumn.isBefore(after).contains(0)); assertFalse(dateColumn.isOnOrAfter(after).contains(0)); assertTrue(t.dateColumn("test").isBefore(after).contains(0)); assertTrue(t.dateColumn("test").isBefore(t.dateColumn("after")).contains(0)); assertTrue(dateColumn.isAfter(before).contains(0)); assertFalse(dateColumn.isOnOrBefore(before).contains(0)); assertTrue(t.dateColumn("test").isAfter(before).contains(0)); assertTrue(t.dateColumn("test").isAfter(t.dateColumn("before")).contains(0)); assertFalse(dateColumn.isNotEqualTo(same).contains(0)); assertFalse(t.dateColumn("test").isNotEqualTo(same).contains(0)); assertFalse(t.dateColumn("test").isNotEqualTo(t.dateColumn("same")).contains(0)); assertTrue(dateColumn.isOnOrBefore(same).contains(0)); assertTrue(dateColumn.isOnOrBefore(after).contains(0)); assertFalse(dateColumn.isOnOrBefore(before).contains(0)); assertTrue(dateColumn.isNotEqualTo(before).contains(0)); assertTrue(dateColumn.isOnOrAfter(same).contains(1)); assertTrue(dateColumn.isOnOrAfter(before).contains(2)); assertFalse(dateColumn.isOnOrAfter(after).contains(2)); assertTrue(dateColumn.isNotEqualTo(after).contains(0)); /* assertTrue(t.dateColumn("test") .isOnOrAfter(t.dateColumn("same")).contains(0)); assertTrue(t.dateColumn("test") .isOnOrAfter(same).contains(0)); assertFalse(t.dateColumn("test") .isOnOrAfter(t.dateColumn("after")).contains(0)); assertFalse(t.dateColumn("test") .isOnOrAfter(after).contains(0)); assertTrue(t.dateColumn("test") .isOnOrBefore(t.dateColumn("same")).contains(0)); assertTrue(t.dateColumn("test") .isOnOrBefore(same).contains(0)); assertTrue(t.dateColumn("test") .isOnOrBefore(t.dateColumn("after")).contains(0)); assertTrue(t.dateColumn("test") .isOnOrBefore(after).contains(0)); assertFalse(t.dateColumn("test") .isOnOrBefore(t.dateColumn("before")).contains(0)); assertFalse(t.dateColumn("test") .isOnOrBefore(before).contains(0)); */ assertTrue(t.dateColumn("test").isNotEqualTo(t.dateColumn("before")).contains(0)); assertTrue(t.dateColumn("test").isNotEqualTo(before).contains(0)); assertFalse(t.dateColumn("test").isNotEqualTo(t.dateColumn("same")).contains(0)); assertFalse(t.dateColumn("test").isNotEqualTo(same).contains(0)); }
Example 19
Source File: PivotTable.java From tablesaw with Apache License 2.0 | 4 votes |
public static Table pivot( Table table, CategoricalColumn<?> column1, CategoricalColumn<?> column2, NumericColumn<?> values, AggregateFunction<?, ?> aggregateFunction) { TableSliceGroup tsg = table.splitOn(column1); Table pivotTable = Table.create("Pivot: " + column1.name() + " x " + column2.name()); pivotTable.addColumns(column1.type().create(column1.name())); List<String> valueColumnNames = getValueColumnNames(table, column2); for (String colName : valueColumnNames) { pivotTable.addColumns(DoubleColumn.create(colName)); } int valueIndex = table.columnIndex(column2); int keyIndex = table.columnIndex(column1); String key; for (TableSlice slice : tsg.getSlices()) { key = String.valueOf(slice.get(0, keyIndex)); pivotTable.column(0).appendCell(key); Map<String, Double> valueMap = getValueMap(column1, column2, values, valueIndex, slice, aggregateFunction); for (String columnName : valueColumnNames) { Double aDouble = valueMap.get(columnName); NumericColumn<?> pivotValueColumn = pivotTable.numberColumn(columnName); if (aDouble == null) { pivotValueColumn.appendMissing(); } else { pivotValueColumn.appendObj(aDouble); } } } return pivotTable; }
Example 20
Source File: HtmlReader.java From tablesaw with Apache License 2.0 | 4 votes |
@Override public Table read(HtmlReadOptions options) throws IOException { Document doc; InputStream inputStream = options.source().inputStream(); if (inputStream != null) { // Reader must support mark, so can't use InputStreamReader // Parse the InputStream directly doc = Jsoup.parse(inputStream, null, ""); } else { doc = Parser.htmlParser().parseInput(options.source().createReader(null), ""); } Elements tables = doc.select("table"); int tableIndex = 0; if (tables.size() != 1) { if (options.tableIndex() != null) { if (options.tableIndex() >= 0 && options.tableIndex() < tables.size()) { tableIndex = options.tableIndex(); } else { throw new IndexOutOfBoundsException( "Table index outside bounds. The URL has " + tables.size() + " tables"); } } else { throw new IllegalStateException( tables.size() + " tables found. When more than one html table is present on the page you must specify the index of the table to read from."); } } Element htmlTable = tables.get(tableIndex); List<String[]> rows = new ArrayList<>(); for (Element row : htmlTable.select("tr")) { Elements headerCells = row.getElementsByTag("th"); Elements cells = row.getElementsByTag("td"); String[] nextLine = Stream.concat(headerCells.stream(), cells.stream()) .map(Element::text) .toArray(String[]::new); rows.add(nextLine); } Table table = Table.create(options.tableName()); if (rows.isEmpty()) { return table; } List<String> columnNames = new ArrayList<>(); if (options.header()) { String[] headerRow = rows.remove(0); for (int i = 0; i < headerRow.length; i++) { columnNames.add(headerRow[i]); } } else { for (int i = 0; i < rows.get(0).length; i++) { columnNames.add("C" + i); } } return TableBuildingUtils.build(columnNames, rows, options); }