Skip to content

Commit 14de84b

Browse files
committed
Added tests and whats new
1 parent 27cc0e2 commit 14de84b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

doc/source/whatsnew/v2.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Plotting
142142

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

148148
Reshaping

pandas/tests/groupby/test_groupby.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,6 +3004,30 @@ def test_groupby_agg_namedagg_with_duplicate_columns():
30043004

30053005
tm.assert_frame_equal(result, expected)
30063006

3007+
class MyDataFrame(DataFrame):
3008+
@property
3009+
def _constructor(self):
3010+
return MyDataFrame
3011+
3012+
@pytest.mark.parametrize("data, agg_dict, expected", [
3013+
pytest.param(
3014+
{"A": [1, 1, 2, 2], "B": [1, 2, 3, 4]},
3015+
{"B": "sum"},
3016+
DataFrame({"B": [3, 7]}, index=Index([1, 2], name="A"))
3017+
),
3018+
pytest.param(
3019+
{"A": [1, 1, 2, 2], "B": [1, 2, 3, 4], "C": [4, 3, 2, 1]},
3020+
{"B": "sum", "C": "mean"},
3021+
DataFrame({"B": [3, 7], "C": [3.5, 1.5]}, index=Index([1, 2], name="A"))
3022+
),
3023+
])
3024+
def test_groupby_agg_preserves_subclass(data, agg_dict, expected):
3025+
# GH#59667
3026+
df = MyDataFrame(data)
3027+
result = df.groupby("A").agg(agg_dict)
3028+
3029+
assert isinstance(result, MyDataFrame)
3030+
tm.assert_frame_equal(result, expected)
30073031

30083032
def test_groupby_multi_index_codes():
30093033
# GH#54347

0 commit comments

Comments
 (0)