Skip to content

Commit 14de3fd

Browse files
ahmedibrhmrhshadrach
andauthoredAug 1, 2022
DEPR: returning tuple when grouping by a list containing single element (pandas-dev#47761)
* DOC pandas-dev#45443 edited the documentation of where/mask functions * DOC pandas-dev#45443 edited the documentation of where/mask functions * Update generic.py * ENH: add suffixes argument to DataFrame.compare pandas-dev#44354 * Edited the tests * space fixing * Update shared_docs.py * Update series.py * Update series.py * invalid argument tests * issue reference * syntax editing * grammar fixing * edit doc * editting doc * Update 02_read_write.rst * Update 02_read_write.rst * Update v1.5.0.rst * Update v1.5.0.rst * np * 1.5.0 rst * created tests for invalid input * space * space * space * editing test * deprecated * syntax * editting existed examples * syntax * edit past tests * editting pivot * ex * editing internal use * pivot * warning expected * warning * ignore doc warning * doc * tests * ignore warning * test * plotting * test * doc * doc * white space * doc * doc * doc * doc * stacklevel * pivot * pivot * cookbook * flake8 * flake8 * what's new * syntax * itr * car names * test edit * fixing tests * fixing tests * flake8 * rst edit * __iter__ edit * flake8 * flake8 * space * test * merge * ignore the type * mypy * type * self.keys * tests * . * . * adding keys * order * attribute * ignores * Update hist.py * ignore * . * . * . * . * . * Update doc/source/whatsnew/v1.5.0.rst Co-authored-by: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com> Co-authored-by: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com>
1 parent 93c2fc2 commit 14de3fd

File tree

8 files changed

+50
-4
lines changed

8 files changed

+50
-4
lines changed
 

‎doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ Other Deprecations
776776
- Deprecated argument ``errors`` for :meth:`Series.mask`, :meth:`Series.where`, :meth:`DataFrame.mask`, and :meth:`DataFrame.where` as ``errors`` had no effect on this methods (:issue:`47728`)
777777
- Deprecated arguments ``*args`` and ``**kwargs`` in :class:`Rolling`, :class:`Expanding`, and :class:`ExponentialMovingWindow` ops. (:issue:`47836`)
778778
- Deprecated unused arguments ``encoding`` and ``verbose`` in :meth:`Series.to_excel` and :meth:`DataFrame.to_excel` (:issue:`47912`)
779+
- Deprecated producing a single element when iterating over a :class:`DataFrameGroupBy` or a :class:`SeriesGroupBy` that has been grouped by a list of length 1; A tuple of length one will be returned instead (:issue:`42795`)
779780

780781
.. ---------------------------------------------------------------------------
781782
.. _whatsnew_150.performance:

‎pandas/core/groupby/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
465465
klass = type(self.obj)
466466

467467
results = []
468-
for name, group in self:
468+
for name, group in self.grouper.get_iterator(
469+
self._selected_obj, axis=self.axis
470+
):
469471
# this setattr is needed for test_transform_lambda_with_datetimetz
470472
object.__setattr__(group, "name", name)
471473
res = func(group, *args, **kwargs)

‎pandas/core/groupby/groupby.py

+14
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT], GroupByIndexingMixin):
645645

646646
axis: int
647647
grouper: ops.BaseGrouper
648+
keys: _KeysArgType | None = None
648649
group_keys: bool | lib.NoDefault
649650

650651
@final
@@ -821,6 +822,19 @@ def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
821822
Generator yielding sequence of (name, subsetted object)
822823
for each group
823824
"""
825+
keys = self.keys
826+
if isinstance(keys, list) and len(keys) == 1:
827+
warnings.warn(
828+
(
829+
"In a future version of pandas, a length 1 "
830+
"tuple will be returned when iterating over a "
831+
"a groupby with a grouper equal to a list of "
832+
"length 1. Don't supply a list with a single grouper "
833+
"to avoid this warning."
834+
),
835+
FutureWarning,
836+
stacklevel=find_stack_level(),
837+
)
824838
return self.grouper.get_iterator(self._selected_obj, axis=self.axis)
825839

826840

‎pandas/core/reshape/merge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _groupby_and_merge(by, left: DataFrame, right: DataFrame, merge_pieces):
150150
if all(item in right.columns for item in by):
151151
rby = right.groupby(by, sort=False)
152152

153-
for key, lhs in lby:
153+
for key, lhs in lby.grouper.get_iterator(lby._selected_obj, axis=lby.axis):
154154

155155
if rby is None:
156156
rhs = right

‎pandas/plotting/_matplotlib/groupby.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
concat,
1717
)
1818

19+
from pandas.plotting._matplotlib.misc import unpack_single_str_list
20+
1921

2022
def create_iter_data_given_by(
2123
data: DataFrame, kind: str = "hist"
@@ -108,7 +110,8 @@ def reconstruct_data_with_by(
108110
1 3.0 4.0 NaN NaN
109111
2 NaN NaN 5.0 6.0
110112
"""
111-
grouped = data.groupby(by)
113+
by_modified = unpack_single_str_list(by)
114+
grouped = data.groupby(by_modified)
112115

113116
data_list = []
114117
for key, group in grouped:

‎pandas/plotting/_matplotlib/hist.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
create_iter_data_given_by,
3434
reformat_hist_y_given_by,
3535
)
36+
from pandas.plotting._matplotlib.misc import unpack_single_str_list
3637
from pandas.plotting._matplotlib.tools import (
3738
create_subplots,
3839
flatten_axes,
@@ -67,7 +68,8 @@ def _args_adjust(self):
6768
# where subplots are created based on by argument
6869
if is_integer(self.bins):
6970
if self.by is not None:
70-
grouped = self.data.groupby(self.by)[self.columns]
71+
by_modified = unpack_single_str_list(self.by)
72+
grouped = self.data.groupby(by_modified)[self.columns]
7173
self.bins = [self._calculate_bins(group) for key, group in grouped]
7274
else:
7375
self.bins = self._calculate_bins(self.data)

‎pandas/plotting/_matplotlib/misc.py

+8
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,11 @@ def r(h):
475475
ax.legend()
476476
ax.grid()
477477
return ax
478+
479+
480+
def unpack_single_str_list(keys):
481+
# GH 42795
482+
if isinstance(keys, list):
483+
if len(keys) == 1 and isinstance(keys[0], str):
484+
keys = keys[0]
485+
return keys

‎pandas/tests/groupby/test_groupby.py

+16
Original file line numberDiff line numberDiff line change
@@ -2795,3 +2795,19 @@ def test_groupby_none_column_name():
27952795
result = df.groupby(by=[None]).sum()
27962796
expected = DataFrame({"b": [2, 5], "c": [9, 13]}, index=Index([1, 2], name=None))
27972797
tm.assert_frame_equal(result, expected)
2798+
2799+
2800+
def test_single_element_list_grouping():
2801+
# GH 42795
2802+
df = DataFrame(
2803+
{"a": [np.nan, 1], "b": [np.nan, 5], "c": [np.nan, 2]}, index=["x", "y"]
2804+
)
2805+
msg = (
2806+
"In a future version of pandas, a length 1 "
2807+
"tuple will be returned when iterating over a "
2808+
"a groupby with a grouper equal to a list of "
2809+
"length 1. Don't supply a list with a single grouper "
2810+
"to avoid this warning."
2811+
)
2812+
with tm.assert_produces_warning(FutureWarning, match=msg):
2813+
values, _ = next(iter(df.groupby(["a"])))

0 commit comments

Comments
 (0)
Please sign in to comment.