Skip to content

Commit

Permalink
BUG: groupby ffill adds labels as extra column (pandas-dev#21521)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam committed Apr 19, 2019
1 parent c8ca6ad commit b48439a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ Groupby/Resample/Rolling
- Bug in :meth:`pandas.core.window.Rolling.min` and :meth:`pandas.core.window.Rolling.max` that caused a memory leak (:issue:`25893`)
- 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`)
- 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`)
- 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`)

Reshaping
^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,8 @@ def _fill(self, direction, limit=None):
"""Overridden method to join grouped columns in output"""
res = super(DataFrameGroupBy, self)._fill(direction, limit=limit)
output = OrderedDict(
(grp.name, grp.grouper) for grp in self.grouper.groupings)
(grp.name, grp.grouper) for grp in self.grouper.groupings
if grp.in_axis)

from pandas import concat
return concat((self._wrap_transformed_output(output), res), axis=1)
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/groupby/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,18 @@ def test_transform_absent_categories(func):
result = getattr(df.y.groupby(df.x), func)()
expected = df.y
assert_series_equal(result, expected)


@pytest.mark.parametrize('func', ['ffill', 'bfill'])
@pytest.mark.parametrize('arg', ['level', 'labels'])
def test_groupby_ffill_index(func, arg):
# GH 21521
df = pd.DataFrame([[0]])
if arg == 'level':
groups = df.groupby(level=0)
elif arg == 'labels':
groups = df.groupby(Series([0]))
result = getattr(groups, func)()
expected = df

assert_frame_equal(result, expected)

0 comments on commit b48439a

Please sign in to comment.