diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 6bb972c21d927..b30aad4091411 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -438,6 +438,7 @@ Groupby/resample/rolling grouped :class:`Series` or :class:`DataFrame` was a :class:`DatetimeIndex`, :class:`TimedeltaIndex` or :class:`PeriodIndex`, and the ``groupby`` method was given a function as its first argument, the function operated on the whole index rather than each element of the index. (:issue:`51979`) +- 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`) - Bug in :meth:`DataFrameGroupBy.agg` with lists not respecting ``as_index=False`` (:issue:`52849`) - 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`) - Bug in :meth:`DataFrameGroupBy.apply` raising a ``TypeError`` when selecting multiple columns and providing a function that returns ``np.ndarray`` results (:issue:`18930`) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 80e7be0fd3c91..1d5fb0fb3873d 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1935,7 +1935,7 @@ def _gotitem(self, key, ndim: int, subset=None): subset = self.obj return DataFrameGroupBy( subset, - self.grouper, + self.keys, axis=self.axis, level=self.level, grouper=self.grouper, @@ -1952,6 +1952,7 @@ def _gotitem(self, key, ndim: int, subset=None): subset = self.obj[key] return SeriesGroupBy( subset, + self.keys, level=self.level, grouper=self.grouper, exclusions=self.exclusions, diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 0c6661b49d917..bf0b646847ed6 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2722,10 +2722,13 @@ def test_groupby_none_column_name(): tm.assert_frame_equal(result, expected) -def test_single_element_list_grouping(): - # GH 42795 +@pytest.mark.parametrize("selection", [None, "a", ["a"]]) +def test_single_element_list_grouping(selection): + # GH#42795, GH#53500 df = DataFrame({"a": [1, 2], "b": [np.nan, 5], "c": [np.nan, 2]}, index=["x", "y"]) - result = [key for key, _ in df.groupby(["a"])] + grouped = df.groupby(["a"]) if selection is None else df.groupby(["a"])[selection] + result = [key for key, _ in grouped] + expected = [(1,), (2,)] assert result == expected