Skip to content

Commit 05308f3

Browse files
committed
TST: Add test for groupby.apply() to ensure metadata preservation in subclassed DataFrames and Series
1 parent e7f8e87 commit 05308f3

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

pandas/tests/groupby/test_groupby_subclass.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ def func2(group):
9797
result = custom_series.groupby(custom_df["c"]).agg(func2)
9898
tm.assert_series_equal(result, expected)
9999

100+
101+
def test_groupby_apply_preserves_metadata():
100102
# GH#62134 - Test that apply() preserves metadata when returning DataFrames/Series
103+
custom_df = tm.SubclassedDataFrame({"a": [1, 2, 3], "b": [1, 1, 2], "c": [7, 8, 9]})
104+
custom_df.testattr = "hello"
105+
101106
def sum_func(group):
102107
assert isinstance(group, tm.SubclassedDataFrame)
103108
assert hasattr(group, "testattr")
@@ -108,6 +113,9 @@ def sum_func(group):
108113
assert hasattr(result, "testattr"), "DataFrame apply() should preserve metadata"
109114
assert result.testattr == "hello"
110115

116+
custom_series = tm.SubclassedSeries([1, 2, 3])
117+
custom_series.testattr = "hello"
118+
111119
def sum_series_func(group):
112120
assert isinstance(group, tm.SubclassedSeries)
113121
assert hasattr(group, "testattr")

0 commit comments

Comments
 (0)