Skip to content
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

Fix GH-29442 DataFrame.groupby doesn't preserve _metadata #35688

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7cc4d53
Fix GH-29442 DataFrame.groupby doesn't preserve _metadata
Japanuspus Aug 12, 2020
0ab766d
Fix most PEP8 issues
Japanuspus Aug 12, 2020
d7b42e3
Fix remaining PEP8 issues
Japanuspus Aug 13, 2020
fbc602c
Apply "black" styling and fix typo
Japanuspus Aug 13, 2020
e9c64ac
Fix import sorting issues reported by `isort`
Japanuspus Aug 13, 2020
ec7fd00
Make testcase more concise
Japanuspus Aug 14, 2020
1405667
Include test from original issue
Japanuspus Aug 27, 2020
8d3d896
Fix metadata handling for `.groupby(...).sum()`
Japanuspus Aug 27, 2020
39e9f33
Apply black format
Japanuspus Aug 27, 2020
7075ef2
Remove xfail decorator for `sum` aggregation test
Japanuspus Sep 23, 2020
05a3365
Merge branch 'master' into BUG_GH29442_frame_groupby_metadata
Japanuspus Oct 1, 2020
4720c97
Move `__finalize__` outsize context handler
Japanuspus Oct 2, 2020
6b290e8
Revert __finalize__ call in DataSplitter
Japanuspus Oct 2, 2020
a2bdc3d
Add performance test counting __finalize__ calls
Japanuspus Oct 2, 2020
2af8447
Add whatsnew entry
Japanuspus Oct 2, 2020
ec3eeb1
Move __finalize__ inside context manager
Japanuspus Oct 2, 2020
5487563
Fix typo in whatsnew entry
Japanuspus Oct 2, 2020
8bfd08d
Revert 1.1.3 whatsnew entry
Japanuspus Oct 6, 2020
7d9320a
Merge branch 'master' into BUG_GH29442_frame_groupby_metadata
Japanuspus Oct 6, 2020
7878041
Add 1.1.4 whatsnew entry
Japanuspus Oct 6, 2020
5548699
Revert all changes not strictly related to issue
Japanuspus Oct 8, 2020
18e8ff5
Use descriptive names for unit tests
Japanuspus Oct 8, 2020
57694ae
Remove extraneous imports
Japanuspus Oct 8, 2020
d3088a3
Merge master into BUG_GH29442_frame_groupby_metadata
Japanuspus Oct 8, 2020
d86ae78
Delete tests for custom metadata
Japanuspus Oct 9, 2020
6227894
Remove reference to internal class from whatsnew
Japanuspus Oct 9, 2020
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 @@ -22,7 +22,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 @@ -1001,8 +1001,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 @@ -1021,8 +1022,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: 15 additions & 1 deletion pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,13 +772,27 @@ def test_categorical_accessor(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",
[
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}