Skip to content

Commit 8215080

Browse files
committed
TST: test groupby.apply() with user-defined function returning an empty chunk
1 parent 739bc4c commit 8215080

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pandas/tests/groupby/test_apply.py

+13
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,19 @@ def test_func(x):
518518
tm.assert_frame_equal(result2, expected2)
519519

520520

521+
def test_groupby_apply_return_empty_chunk():
522+
# GH 22221: apply filter which returns some empty groups
523+
df = pd.DataFrame(dict(value=[0, 1], group=['filled', 'empty']))
524+
groups = df.groupby('group')
525+
result = groups.apply(lambda group: group[group.value != 1]['value'])
526+
expected = pd.Series([0], name='value',
527+
index=MultiIndex.from_product([['empty', 'filled'],
528+
[0]],
529+
names=['group', None]
530+
).drop('empty'))
531+
tm.assert_series_equal(result, expected)
532+
533+
521534
def test_apply_with_mixed_types():
522535
# gh-20949
523536
df = pd.DataFrame({'A': 'a a b'.split(), 'B': [1, 2, 3], 'C': [4, 6, 5]})

0 commit comments

Comments
 (0)