Skip to content

FIX groupby with column selection not returning tuple when grouping by list of a single element #53517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -435,6 +435,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 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
9 changes: 7 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,8 +901,13 @@ def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
FutureWarning,
stacklevel=find_stack_level(),
)
if isinstance(keys, list) and len(keys) == 1:
# GH#42795 - when keys is a list, return tuples even when length is 1
if (
isinstance(keys, list)
and len(keys) == 1
or isinstance(keys, ops.BaseGrouper)
and len(keys.names) == 1
):
# GH#42795, GH#53500 - if groupby by list of one, still return tuples
result = (((key,), group) for key, group in result)
return result

Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2723,11 +2723,16 @@ def test_groupby_none_column_name():


def test_single_element_list_grouping():
# GH 42795
# 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"])

result1 = [key for key, _ in grouped]
result2 = [key for key, _ in grouped[["a", "b", "c"]]]
expected = [(1,), (2,)]
assert result == expected

assert result1 == expected
assert result2 == expected


def test_groupby_string_dtype():
Expand Down