Skip to content

Commit 010989d

Browse files
authored
BUG: iterating on a subset of columns in a GroupBy object (#44821) (#44947)
1 parent 8e38f28 commit 010989d

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ Groupby/resample/rolling
814814
- Bug in :meth:`GroupBy.nth` failing on ``axis=1`` (:issue:`43926`)
815815
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not respecting right bound on centered datetime-like windows, if the index contain duplicates (:issue:`3944`)
816816
- Bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` when using a :class:`pandas.api.indexers.BaseIndexer` subclass that returned unequal start and end arrays would segfault instead of raising a ``ValueError`` (:issue:`44470`)
817+
- Fixed bug in :meth:`GroupBy.__iter__` after selecting a subset of columns in a :class:`GroupBy` object, which returned all columns instead of the chosen subset (:issue:`#44821`)
817818
- Bug in :meth:`Groupby.rolling` when non-monotonic data passed, fails to correctly raise ``ValueError`` (:issue:`43909`)
818819
- Fixed bug where grouping by a :class:`Series` that has a categorical data type and length unequal to the axis of grouping raised ``ValueError`` (:issue:`44179`)
819820

pandas/core/groupby/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
758758
Generator yielding sequence of (name, subsetted object)
759759
for each group
760760
"""
761-
return self.grouper.get_iterator(self.obj, axis=self.axis)
761+
return self.grouper.get_iterator(self._selected_obj, axis=self.axis)
762762

763763

764764
# To track operations that expand dimensions, like ohlc

pandas/tests/groupby/test_indexing.py

+11
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,17 @@ def test_column_axis(column_group_df):
288288
tm.assert_frame_equal(result, expected)
289289

290290

291+
def test_columns_on_iter():
292+
# GitHub issue #44821
293+
df = pd.DataFrame({k: range(10) for k in "ABC"})
294+
295+
# Group-by and select columns
296+
cols = ["A", "B"]
297+
for _, dg in df.groupby(df.A < 4)[cols]:
298+
tm.assert_index_equal(dg.columns, pd.Index(cols))
299+
assert "C" not in dg.columns
300+
301+
291302
@pytest.mark.parametrize("func", [list, pd.Index, pd.Series, np.array])
292303
def test_groupby_duplicated_columns(func):
293304
# GH#44924

pandas/tests/window/test_base_indexer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ def test_rolling_groupby_with_fixed_forward_many(group_keys, window_size):
437437
result = df.groupby("a")["b"].rolling(window=indexer, min_periods=1).sum()
438438
result.index.names = ["a", "c"]
439439

440-
groups = df.groupby("a")[["a", "b"]]
440+
groups = df.groupby("a")[["a", "b", "c"]]
441441
manual = concat(
442442
[
443443
g.assign(

0 commit comments

Comments
 (0)