Skip to content

Commit

Permalink
GroupBy.rowNumber() #16
Browse files Browse the repository at this point in the history
* testing combination of sort and row number
  • Loading branch information
andrus committed Mar 31, 2019
1 parent 28f090f commit 6c952dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dflib/src/main/java/com/nhl/dflib/GroupBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,31 @@ public <V extends Comparable<? super V>> GroupBy sort(RowToValueMapper<V> sortKe
return new GroupBy(ungrouped, sorted);
}

public GroupBy sort(String column, boolean ascending) {

Comparator<RowProxy> comparator = Sorters.sorter(ungrouped.getColumnsIndex(), column, ascending);
Map<Object, Series<Integer>> sorted = new LinkedHashMap<>((int) (groupsIndex.size() / 0.75));

for (Map.Entry<Object, Series<Integer>> e : groupsIndex.entrySet()) {
Series<Integer> sortedGroup = new IndexSorter(ungrouped, e.getValue()).sortIndex(comparator);
sorted.put(e.getKey(), sortedGroup);
}

return new GroupBy(ungrouped, sorted);
}

public GroupBy sort(int column, boolean ascending) {
Comparator<RowProxy> comparator = Sorters.sorter(ungrouped.getColumnsIndex(), column, ascending);
Map<Object, Series<Integer>> sorted = new LinkedHashMap<>((int) (groupsIndex.size() / 0.75));

for (Map.Entry<Object, Series<Integer>> e : groupsIndex.entrySet()) {
Series<Integer> sortedGroup = new IndexSorter(ungrouped, e.getValue()).sortIndex(comparator);
sorted.put(e.getKey(), sortedGroup);
}

return new GroupBy(ungrouped, sorted);
}

public GroupBy sort(String[] columns, boolean[] ascending) {
if (columns.length == 0) {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@ public void testGroupBy_RowNumbers2() {
Series<Integer> rn = df.group("a").rowNumbers();
new SeriesAsserts(rn).expectData(0, 0, 1, 2, 0);
}

@Test
public void testGroupBy_RowNumbers_Sort() {
Index i = Index.forLabels("a", "b", "c");
DataFrame df = createDf(i,
3, "x", "m",
0, "y", "n",
3, "z", "k",
3, "a", "f",
1, "x", "s");

Series<Integer> rn = df.group("a").sort("b", true).rowNumbers();
new SeriesAsserts(rn).expectData(1, 0, 2, 0, 0);
}
}

0 comments on commit 6c952dc

Please sign in to comment.