Skip to content

Commit 99db9af

Browse files
BUG: fix subclass metadata preservation in groupby column selection (#56761)
1 parent 7d61e32 commit 99db9af

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,7 @@ Groupby/resample/rolling
873873
- Bug in :meth:`DataFrame.asfreq` and :meth:`Series.asfreq` with a :class:`DatetimeIndex` with non-nanosecond resolution incorrectly converting to nanosecond resolution (:issue:`55958`)
874874
- Bug in :meth:`DataFrame.ewm` when passed ``times`` with non-nanosecond ``datetime64`` or :class:`DatetimeTZDtype` dtype (:issue:`56262`)
875875
- Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby` where grouping by a combination of ``Decimal`` and NA values would fail when ``sort=True`` (:issue:`54847`)
876+
- Bug in :meth:`DataFrame.groupby` for DataFrame subclasses when selecting a subset of columns to apply the function to (:issue:`56761`)
876877
- Bug in :meth:`DataFrame.resample` not respecting ``closed`` and ``label`` arguments for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55282`)
877878
- Bug in :meth:`DataFrame.resample` when resampling on a :class:`ArrowDtype` of ``pyarrow.timestamp`` or ``pyarrow.duration`` type (:issue:`55989`)
878879
- Bug in :meth:`DataFrame.resample` where bin edges were not correct for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55281`)

pandas/core/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4016,7 +4016,9 @@ def _getitem_nocopy(self, key: list):
40164016
copy=False,
40174017
only_slice=True,
40184018
)
4019-
return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes)
4019+
result = self._constructor_from_mgr(new_mgr, axes=new_mgr.axes)
4020+
result = result.__finalize__(self)
4021+
return result
40204022

40214023
def __getitem__(self, key):
40224024
check_dict_or_set_indexers(key)

pandas/tests/groupby/test_groupby_subclass.py

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def test_groupby_preserves_metadata():
6969
def func(group):
7070
assert isinstance(group, tm.SubclassedDataFrame)
7171
assert hasattr(group, "testattr")
72+
assert group.testattr == "hello"
7273
return group.testattr
7374

7475
msg = "DataFrameGroupBy.apply operated on the grouping columns"
@@ -79,6 +80,13 @@ def func(group):
7980
expected = tm.SubclassedSeries(["hello"] * 3, index=Index([7, 8, 9], name="c"))
8081
tm.assert_series_equal(result, expected)
8182

83+
result = custom_df.groupby("c").apply(func, include_groups=False)
84+
tm.assert_series_equal(result, expected)
85+
86+
# https://github.com/pandas-dev/pandas/pull/56761
87+
result = custom_df.groupby("c")[["a", "b"]].apply(func)
88+
tm.assert_series_equal(result, expected)
89+
8290
def func2(group):
8391
assert isinstance(group, tm.SubclassedSeries)
8492
assert hasattr(group, "testattr")

0 commit comments

Comments
 (0)