Skip to content

Commit 8d3d896

Browse files
committed
Fix metadata handling for .groupby(...).sum()
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
1 parent 1405667 commit 8d3d896

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

pandas/core/groupby/groupby.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,9 @@ def _agg_general(
993993
):
994994
with _group_selection_context(self):
995995
# try a cython aggregation if we can
996+
result = None
996997
try:
997-
return self._cython_agg_general(
998+
result = self._cython_agg_general(
998999
how=alias,
9991000
alt=npfunc,
10001001
numeric_only=numeric_only,
@@ -1009,10 +1010,10 @@ def _agg_general(
10091010
# raised in _get_cython_function, in some cases can
10101011
# be trimmed by implementing cython funcs for more dtypes
10111012
pass
1012-
10131013
# apply a non-cython aggregation
1014-
result = self.aggregate(lambda x: npfunc(x, axis=self.axis))
1015-
return result
1014+
if result is None:
1015+
result = self.aggregate(lambda x: npfunc(x, axis=self.axis))
1016+
return result.__finalize__(self.obj, method='groupby')
10161017

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

0 commit comments

Comments
 (0)