Skip to content

Commit 002b2c3

Browse files
TomAugspurgerjreback
authored andcommitted
REGR: Group empty Series (#27194)
1 parent 37b2268 commit 002b2c3

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pandas/core/groupby/grouper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,11 @@ def is_in_obj(gpr):
606606

607607
groupings.append(ping)
608608

609-
if len(groupings) == 0:
609+
if len(groupings) == 0 and len(obj):
610610
raise ValueError('No group keys passed!')
611+
elif len(groupings) == 0:
612+
groupings.append(Grouping(Index([], dtype='int'),
613+
np.array([], dtype=np.intp)))
611614

612615
# create the internals grouper
613616
grouper = BaseGrouper(group_axis, groupings, sort=sort, mutated=mutated)

pandas/tests/groupby/test_grouping.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,27 @@ def test_evaluate_with_empty_groups(self, func, expected):
569569
result = getattr(g[2], func)(lambda x: x)
570570
assert_series_equal(result, expected)
571571

572+
def test_groupby_empty(self):
573+
# https://github.com/pandas-dev/pandas/issues/27190
574+
s = pd.Series([], name='name')
575+
gr = s.groupby([])
576+
577+
result = gr.mean()
578+
tm.assert_series_equal(result, s)
579+
580+
# check group properties
581+
assert len(gr.grouper.groupings) == 1
582+
tm.assert_numpy_array_equal(gr.grouper.group_info[0],
583+
np.array([], dtype=np.dtype("intp")))
584+
585+
tm.assert_numpy_array_equal(gr.grouper.group_info[1],
586+
np.array([], dtype=np.dtype('int')))
587+
588+
assert gr.grouper.group_info[2] == 0
589+
590+
# check name
591+
assert s.groupby(s).grouper.names == ['name']
592+
572593

573594
# get_group
574595
# --------------------------------

0 commit comments

Comments
 (0)