Skip to content

Backport PR #35688 on branch 1.1.x: Fix GH-29442 DataFrame.groupby doesn't preserve _metadata #37122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Fixed regressions

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

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,10 @@ def _agg_general(
):
self._set_group_selection()

result = None
# try a cython aggregation if we can
try:
return self._cython_agg_general(
result = self._cython_agg_general(
how=alias, alt=npfunc, numeric_only=numeric_only, min_count=min_count,
)
except DataError:
Expand All @@ -1012,8 +1013,9 @@ def _agg_general(
raise

# 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
16 changes: 13 additions & 3 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,21 +770,31 @@ def test_categorical_accessor(method):
# Groupby


@pytest.mark.parametrize(
"obj", [pd.Series([0, 0]), pd.DataFrame({"A": [0, 1], "B": [1, 2]})]
)
@pytest.mark.parametrize(
"method", [operator.methodcaller("sum"), lambda x: x.agg("sum")],
)
def test_groupby_finalize(obj, method):
obj.attrs = {"a": 1}
result = method(obj.groupby([0, 0]))
assert result.attrs == {"a": 1}


@pytest.mark.parametrize(
"obj", [pd.Series([0, 0]), pd.DataFrame({"A": [0, 1], "B": [1, 2]})]
)
@pytest.mark.parametrize(
"method",
[
operator.methodcaller("sum"),
lambda x: x.agg("sum"),
lambda x: x.agg(["sum", "count"]),
lambda x: x.transform(lambda y: y),
lambda x: x.apply(lambda y: y),
],
)
@not_implemented_mark
def test_groupby(obj, method):
def test_groupby_finalize_not_implemented(obj, method):
obj.attrs = {"a": 1}
result = method(obj.groupby([0, 0]))
assert result.attrs == {"a": 1}