Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove precondition on aggregate such that empty slices are allowed #795

Merged
merged 1 commit into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions core/src/main/java/tech/tablesaw/table/TableSliceGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package tech.tablesaw.table;

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
Expand Down Expand Up @@ -187,7 +186,6 @@ public Table aggregate(String colName1, AggregateFunction<?, ?>... functions) {
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public Table aggregate(ListMultimap<String, AggregateFunction<?, ?>> functions) {
Preconditions.checkArgument(!getSlices().isEmpty());
Table groupTable = summaryTableName(sourceTable);
StringColumn groupColumn = StringColumn.create("Group");
groupTable.addColumns(groupColumn);
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/java/tech/tablesaw/table/TableSliceGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import org.apache.commons.math3.stat.StatUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -139,4 +140,29 @@ public void aggregateWithMultipleColumns() {
group.aggregate(ImmutableListMultimap.of("approval", exaggerate, "approval2", exaggerate));
assertEquals(aggregated.rowCount(), group.size());
}

/**
* Make sure that aggregations are allowed on empty tables. They should however just create new
* empty tables.
*
* <p>see <a href="https://github.com/jtablesaw/tablesaw/issues/785">Issue#785</a>
*/
@Test
public void aggregateWithEmptyResult() {
// drop all rows in order to carry out aggregation on an empty table
table = table.dropRows(IntStream.range(0, table.column(0).size()).toArray());

TableSliceGroup group = StandardTableSliceGroup.create(table, table.categoricalColumn("who"));
Table aggregated = group.aggregate("approval", exaggerate);
assertEquals(0, aggregated.rowCount(), "result should be empty");
assertEquals(2, aggregated.columnCount()); // 1 original column + the aggregation column

table.addColumns(table.categoricalColumn("approval").copy().setName("approval2"));
group = StandardTableSliceGroup.create(table, table.categoricalColumn("who"));

aggregated =
group.aggregate(ImmutableListMultimap.of("approval", exaggerate, "approval2", exaggerate));
assertEquals(0, aggregated.rowCount(), "result should be empty");
assertEquals(3, aggregated.columnCount()); // 2 original columns + the aggregation column
}
}