Skip to content

Commit 5ceebe9

Browse files
authored
BUG: groupby with column selection not returning tuple when grouping by list of a single element (#53517)
1 parent 2b69700 commit 5ceebe9

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ Groupby/resample/rolling
439439
grouped :class:`Series` or :class:`DataFrame` was a :class:`DatetimeIndex`, :class:`TimedeltaIndex`
440440
or :class:`PeriodIndex`, and the ``groupby`` method was given a function as its first argument,
441441
the function operated on the whole index rather than each element of the index. (:issue:`51979`)
442+
- Bug in :meth:`DataFrame.groupby` with column selection on the resulting groupby object not returning names as tuples when grouping by a list of a single element. (:issue:`53500`)
442443
- Bug in :meth:`DataFrameGroupBy.agg` with lists not respecting ``as_index=False`` (:issue:`52849`)
443444
- Bug in :meth:`DataFrameGroupBy.apply` causing an error to be raised when the input :class:`DataFrame` was subset as a :class:`DataFrame` after groupby (``[['a']]`` and not ``['a']``) and the given callable returned :class:`Series` that were not all indexed the same. (:issue:`52444`)
444445
- Bug in :meth:`DataFrameGroupBy.apply` raising a ``TypeError`` when selecting multiple columns and providing a function that returns ``np.ndarray`` results (:issue:`18930`)

pandas/core/groupby/generic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ def _gotitem(self, key, ndim: int, subset=None):
19351935
subset = self.obj
19361936
return DataFrameGroupBy(
19371937
subset,
1938-
self.grouper,
1938+
self.keys,
19391939
axis=self.axis,
19401940
level=self.level,
19411941
grouper=self.grouper,
@@ -1952,6 +1952,7 @@ def _gotitem(self, key, ndim: int, subset=None):
19521952
subset = self.obj[key]
19531953
return SeriesGroupBy(
19541954
subset,
1955+
self.keys,
19551956
level=self.level,
19561957
grouper=self.grouper,
19571958
exclusions=self.exclusions,

pandas/tests/groupby/test_groupby.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,10 +2722,13 @@ def test_groupby_none_column_name():
27222722
tm.assert_frame_equal(result, expected)
27232723

27242724

2725-
def test_single_element_list_grouping():
2726-
# GH 42795
2725+
@pytest.mark.parametrize("selection", [None, "a", ["a"]])
2726+
def test_single_element_list_grouping(selection):
2727+
# GH#42795, GH#53500
27272728
df = DataFrame({"a": [1, 2], "b": [np.nan, 5], "c": [np.nan, 2]}, index=["x", "y"])
2728-
result = [key for key, _ in df.groupby(["a"])]
2729+
grouped = df.groupby(["a"]) if selection is None else df.groupby(["a"])[selection]
2730+
result = [key for key, _ in grouped]
2731+
27292732
expected = [(1,), (2,)]
27302733
assert result == expected
27312734

0 commit comments

Comments
 (0)