Skip to content

Commit

Permalink
Fix metadata handling for .groupby(...).sum()
Browse files Browse the repository at this point in the history
In order to propagate metadata fields, the `__finalize__` method must be
called for the resulting DataFrame with a reference to input. By
implementing this in `_GroupBy._agg_general`, this is performed as late
as possible for the `.sum()` (and similar) code-paths.

Fixes #GH-29442
  • Loading branch information
Japanuspus committed Aug 27, 2020
1 parent 1405667 commit 8d3d896
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,8 +993,9 @@ def _agg_general(
):
with _group_selection_context(self):
# try a cython aggregation if we can
result = None
try:
return self._cython_agg_general(
result = self._cython_agg_general(
how=alias,
alt=npfunc,
numeric_only=numeric_only,
Expand All @@ -1009,10 +1010,10 @@ def _agg_general(
# raised in _get_cython_function, in some cases can
# be trimmed by implementing cython funcs for more dtypes
pass

# apply a non-cython aggregation
result = self.aggregate(lambda x: npfunc(x, axis=self.axis))
return result
if result is None:
result = self.aggregate(lambda x: npfunc(x, axis=self.axis))
return result.__finalize__(self.obj, method='groupby')

def _cython_agg_general(
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
Expand Down

0 comments on commit 8d3d896

Please sign in to comment.