Skip to content

Commit

Permalink
Added tests and whats new
Browse files Browse the repository at this point in the history
  • Loading branch information
AshmitGupta committed Sep 1, 2024
1 parent 27cc0e2 commit 14de84b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Plotting

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
-
- Bug in :meth:`DataFrame.groupby` followed by :meth:`DataFrameGroupBy.agg` not preserving subclass type of the original DataFrame (:issue:`59667`)
-

Reshaping
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3004,6 +3004,30 @@ def test_groupby_agg_namedagg_with_duplicate_columns():

tm.assert_frame_equal(result, expected)

class MyDataFrame(DataFrame):
@property
def _constructor(self):
return MyDataFrame

@pytest.mark.parametrize("data, agg_dict, expected", [
pytest.param(
{"A": [1, 1, 2, 2], "B": [1, 2, 3, 4]},
{"B": "sum"},
DataFrame({"B": [3, 7]}, index=Index([1, 2], name="A"))
),
pytest.param(
{"A": [1, 1, 2, 2], "B": [1, 2, 3, 4], "C": [4, 3, 2, 1]},
{"B": "sum", "C": "mean"},
DataFrame({"B": [3, 7], "C": [3.5, 1.5]}, index=Index([1, 2], name="A"))
),
])
def test_groupby_agg_preserves_subclass(data, agg_dict, expected):
# GH#59667
df = MyDataFrame(data)
result = df.groupby("A").agg(agg_dict)

assert isinstance(result, MyDataFrame)
tm.assert_frame_equal(result, expected)

def test_groupby_multi_index_codes():
# GH#54347
Expand Down

0 comments on commit 14de84b

Please sign in to comment.