Skip to content

Commit e7ed4ca

Browse files
authored
DEPR: Enforce DataFrameGroupBy.__iter__ returning tuples of length 1 (#50064)
1 parent 4c5db2e commit e7ed4ca

File tree

3 files changed

+9
-26
lines changed

3 files changed

+9
-26
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ Removal of prior version deprecations/changes
596596
- Enforced deprecation of silently dropping nuisance columns in groupby and resample operations when ``numeric_only=False`` (:issue:`41475`)
597597
- Changed default of ``numeric_only`` in various :class:`.DataFrameGroupBy` methods; all methods now default to ``numeric_only=False`` (:issue:`46072`)
598598
- Changed default of ``numeric_only`` to ``False`` in :class:`.Resampler` methods (:issue:`47177`)
599-
-
599+
- When providing a list of columns of length one to :meth:`DataFrame.groupby`, the keys that are returned by iterating over the resulting :class:`DataFrameGroupBy` object will now be tuples of length one (:issue:`47761`)
600600

601601
.. ---------------------------------------------------------------------------
602602
.. _whatsnew_200.performance:

pandas/core/groupby/groupby.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class providing the base-class of operations.
7070
cache_readonly,
7171
doc,
7272
)
73-
from pandas.util._exceptions import find_stack_level
7473

7574
from pandas.core.dtypes.cast import ensure_dtype_can_hold_na
7675
from pandas.core.dtypes.common import (
@@ -832,19 +831,11 @@ def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
832831
for each group
833832
"""
834833
keys = self.keys
834+
result = self.grouper.get_iterator(self._selected_obj, axis=self.axis)
835835
if isinstance(keys, list) and len(keys) == 1:
836-
warnings.warn(
837-
(
838-
"In a future version of pandas, a length 1 "
839-
"tuple will be returned when iterating over a "
840-
"groupby with a grouper equal to a list of "
841-
"length 1. Don't supply a list with a single grouper "
842-
"to avoid this warning."
843-
),
844-
FutureWarning,
845-
stacklevel=find_stack_level(),
846-
)
847-
return self.grouper.get_iterator(self._selected_obj, axis=self.axis)
836+
# GH#42795 - when keys is a list, return tuples even when length is 1
837+
result = (((key,), group) for key, group in result)
838+
return result
848839

849840

850841
# To track operations that expand dimensions, like ohlc

pandas/tests/groupby/test_groupby.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,18 +2743,10 @@ def test_groupby_none_column_name():
27432743

27442744
def test_single_element_list_grouping():
27452745
# GH 42795
2746-
df = DataFrame(
2747-
{"a": [np.nan, 1], "b": [np.nan, 5], "c": [np.nan, 2]}, index=["x", "y"]
2748-
)
2749-
msg = (
2750-
"In a future version of pandas, a length 1 "
2751-
"tuple will be returned when iterating over "
2752-
"a groupby with a grouper equal to a list of "
2753-
"length 1. Don't supply a list with a single grouper "
2754-
"to avoid this warning."
2755-
)
2756-
with tm.assert_produces_warning(FutureWarning, match=msg):
2757-
values, _ = next(iter(df.groupby(["a"])))
2746+
df = DataFrame({"a": [1, 2], "b": [np.nan, 5], "c": [np.nan, 2]}, index=["x", "y"])
2747+
result = [key for key, _ in df.groupby(["a"])]
2748+
expected = [(1,), (2,)]
2749+
assert result == expected
27582750

27592751

27602752
@pytest.mark.parametrize("func", ["sum", "cumsum", "cumprod", "prod"])

0 commit comments

Comments
 (0)