Skip to content

Commit

Permalink
BUG: groupby with column selection not returning tuple when groupin…
Browse files Browse the repository at this point in the history
…g by list of a single element (pandas-dev#53517)
  • Loading branch information
Charlie-XIAO authored and im-vinicius committed Jul 8, 2023
1 parent 17cd454 commit 365f287
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,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`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 365f287

Please sign in to comment.