Skip to content

Commit c202736

Browse files
Backport PR #35688 on branch 1.1.x: Fix GH-29442 DataFrame.groupby doesn't preserve _metadata (#37122)
Co-authored-by: Janus <[email protected]>
1 parent 6ecd265 commit c202736

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

doc/source/whatsnew/v1.1.4.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Fixed regressions
2727

2828
Bug fixes
2929
~~~~~~~~~
30-
-
30+
- Bug causing ``groupby(...).sum()`` and similar to not preserve metadata (:issue:`29442`)
3131

3232
.. ---------------------------------------------------------------------------
3333

pandas/core/groupby/groupby.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,10 @@ def _agg_general(
994994
):
995995
self._set_group_selection()
996996

997+
result = None
997998
# try a cython aggregation if we can
998999
try:
999-
return self._cython_agg_general(
1000+
result = self._cython_agg_general(
10001001
how=alias, alt=npfunc, numeric_only=numeric_only, min_count=min_count,
10011002
)
10021003
except DataError:
@@ -1012,8 +1013,9 @@ def _agg_general(
10121013
raise
10131014

10141015
# apply a non-cython aggregation
1015-
result = self.aggregate(lambda x: npfunc(x, axis=self.axis))
1016-
return result
1016+
if result is None:
1017+
result = self.aggregate(lambda x: npfunc(x, axis=self.axis))
1018+
return result.__finalize__(self.obj, method="groupby")
10171019

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

pandas/tests/generic/test_finalize.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -770,21 +770,31 @@ def test_categorical_accessor(method):
770770
# Groupby
771771

772772

773+
@pytest.mark.parametrize(
774+
"obj", [pd.Series([0, 0]), pd.DataFrame({"A": [0, 1], "B": [1, 2]})]
775+
)
776+
@pytest.mark.parametrize(
777+
"method", [operator.methodcaller("sum"), lambda x: x.agg("sum")],
778+
)
779+
def test_groupby_finalize(obj, method):
780+
obj.attrs = {"a": 1}
781+
result = method(obj.groupby([0, 0]))
782+
assert result.attrs == {"a": 1}
783+
784+
773785
@pytest.mark.parametrize(
774786
"obj", [pd.Series([0, 0]), pd.DataFrame({"A": [0, 1], "B": [1, 2]})]
775787
)
776788
@pytest.mark.parametrize(
777789
"method",
778790
[
779-
operator.methodcaller("sum"),
780-
lambda x: x.agg("sum"),
781791
lambda x: x.agg(["sum", "count"]),
782792
lambda x: x.transform(lambda y: y),
783793
lambda x: x.apply(lambda y: y),
784794
],
785795
)
786796
@not_implemented_mark
787-
def test_groupby(obj, method):
797+
def test_groupby_finalize_not_implemented(obj, method):
788798
obj.attrs = {"a": 1}
789799
result = method(obj.groupby([0, 0]))
790800
assert result.attrs == {"a": 1}

0 commit comments

Comments
 (0)