Skip to content

Commit

Permalink
unify group and groups
Browse files Browse the repository at this point in the history
  • Loading branch information
papajohn committed Feb 6, 2017
1 parent 6217f8d commit a4afbb0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions datascience/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,11 @@ def group(self, column_or_label, collect=None):
Rectangular | | 27 | 4.7
Round | | 13 | 4.05
"""
# Assume that a call to group with a list of labels is a call to groups
if _is_non_string_iterable(column_or_label) and \
len(column_or_label) != self._num_rows:
return self.groups(column_or_label, collect)

self = self.copy(shallow=True)
collect = _zero_on_type_error(collect)

Expand Down Expand Up @@ -998,6 +1003,10 @@ def groups(self, labels, collect=None):
Green | Round | 2 | 1
Red | Round | 11 | 3.05
"""
# Assume that a call to groups with one label is a call to group
if not _is_non_string_iterable(labels):
return self.group(labels, collect=collect)

collect = _zero_on_type_error(collect)
columns = []
labels = self._as_labels(labels)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,18 @@ def test_groups(t):
10 | False | 1
""")

def test_groups_using_group(t):
t = t.copy()
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
test = t.group(['points', 'early'])
assert_equal(test, """
points | early | count
1 | False | 1
1 | True | 1
2 | True | 2
10 | False | 1
""")

def test_groups_list(t):
t = t.copy()
Expand Down Expand Up @@ -870,6 +882,15 @@ def test_group_no_new_column(table):
z | 1 | 10
""")

def test_group_using_groups(table):
table.groups(1)
assert_equal(table, """
letter | count | points
a | 9 | 1
b | 3 | 2
c | 3 | 2
z | 1 | 10
""")

def test_stack(table):
test = table.stack(key='letter')
Expand Down

0 comments on commit a4afbb0

Please sign in to comment.