Skip to content

Commit b48439a

Browse files
author
Adam
committed
BUG: groupby ffill adds labels as extra column (pandas-dev#21521)
1 parent c8ca6ad commit b48439a

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ Groupby/Resample/Rolling
389389
- Bug in :meth:`pandas.core.window.Rolling.min` and :meth:`pandas.core.window.Rolling.max` that caused a memory leak (:issue:`25893`)
390390
- Bug in :meth:`pandas.core.groupby.GroupBy.idxmax` and :meth:`pandas.core.groupby.GroupBy.idxmin` with datetime column would return incorrect dtype (:issue:`25444`, :issue:`15306`)
391391
- Bug in :meth:`pandas.core.groupby.GroupBy.cumsum`, :meth:`pandas.core.groupby.GroupBy.cumprod`, :meth:`pandas.core.groupby.GroupBy.cummin` and :meth:`pandas.core.groupby.GroupBy.cummax` with categorical column having absent categories, would return incorrect result or segfault (:issue:`16771`)
392+
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.ffill` and :meth:`pandas.core.groupby.DataFrameGroupBy.bfill` when group labels are not in frame, would concat them with the return value. (:issue:`21521`)
392393

393394
Reshaping
394395
^^^^^^^^^

pandas/core/groupby/generic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,8 @@ def _fill(self, direction, limit=None):
14731473
"""Overridden method to join grouped columns in output"""
14741474
res = super(DataFrameGroupBy, self)._fill(direction, limit=limit)
14751475
output = OrderedDict(
1476-
(grp.name, grp.grouper) for grp in self.grouper.groupings)
1476+
(grp.name, grp.grouper) for grp in self.grouper.groupings
1477+
if grp.in_axis)
14771478

14781479
from pandas import concat
14791480
return concat((self._wrap_transformed_output(output), res), axis=1)

pandas/tests/groupby/test_transform.py

+15
Original file line numberDiff line numberDiff line change
@@ -880,3 +880,18 @@ def test_transform_absent_categories(func):
880880
result = getattr(df.y.groupby(df.x), func)()
881881
expected = df.y
882882
assert_series_equal(result, expected)
883+
884+
885+
@pytest.mark.parametrize('func', ['ffill', 'bfill'])
886+
@pytest.mark.parametrize('arg', ['level', 'labels'])
887+
def test_groupby_ffill_index(func, arg):
888+
# GH 21521
889+
df = pd.DataFrame([[0]])
890+
if arg == 'level':
891+
groups = df.groupby(level=0)
892+
elif arg == 'labels':
893+
groups = df.groupby(Series([0]))
894+
result = getattr(groups, func)()
895+
expected = df
896+
897+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)