Skip to content
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

DEPR: returning tuple when grouping by a list containing single element #47761

Merged
merged 129 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
54c5068
DOC #45443 edited the documentation of where/mask functions
ahmedibrhm Jul 8, 2022
2951fb1
DOC #45443 edited the documentation of where/mask functions
ahmedibrhm Jul 8, 2022
6335204
Merge branch 'main' into main
ahmedibrhm Jul 8, 2022
2245def
Merge branch 'main' of https://github.com/ahmedibrhm/pandas
ahmedibrhm Jul 8, 2022
330eda8
Merge branch 'main' of https://github.com/pandas-dev/pandas
ahmedibrhm Jul 8, 2022
8afd6a1
Update generic.py
ahmedibrhm Jul 8, 2022
edc9ff4
Merge branch 'main' of https://github.com/ahmedibrhm/pandas
ahmedibrhm Jul 8, 2022
a326359
ENH: add suffixes argument to DataFrame.compare #44354
ahmedibrhm Jul 8, 2022
d9c4ca9
Edited the tests
ahmedibrhm Jul 8, 2022
1c54472
space fixing
ahmedibrhm Jul 8, 2022
ad7fb76
Merge branch 'main' into issue2
ahmedibrhm Jul 8, 2022
4d34821
Update shared_docs.py
ahmedibrhm Jul 8, 2022
8fb6aa2
Update series.py
ahmedibrhm Jul 8, 2022
1e33dea
Update series.py
ahmedibrhm Jul 8, 2022
ae6c75a
invalid argument tests
ahmedibrhm Jul 8, 2022
ee10dd3
issue reference
ahmedibrhm Jul 8, 2022
077d274
syntax editing
ahmedibrhm Jul 8, 2022
3b2ef9e
Merge branch 'main' into issue2
ahmedibrhm Jul 9, 2022
d0289e5
grammar fixing
ahmedibrhm Jul 10, 2022
bd45e06
edit doc
ahmedibrhm Jul 10, 2022
a13b319
editting doc
ahmedibrhm Jul 10, 2022
f32d7cf
Update 02_read_write.rst
ahmedibrhm Jul 10, 2022
6396583
Update 02_read_write.rst
ahmedibrhm Jul 10, 2022
e754e15
Update v1.5.0.rst
ahmedibrhm Jul 10, 2022
8f67c9f
Update v1.5.0.rst
ahmedibrhm Jul 10, 2022
fa4e26b
Merge branch 'main' into issue2
ahmedibrhm Jul 10, 2022
580773d
np
ahmedibrhm Jul 13, 2022
15a9a57
Merge branch 'pandas-dev:main' into issue2
ahmedibrhm Jul 13, 2022
a4fca56
1.5.0 rst
ahmedibrhm Jul 13, 2022
a468714
Merge branch 'main' into issue2
ahmedibrhm Jul 14, 2022
bc209bb
created tests for invalid input
ahmedibrhm Jul 16, 2022
ff014e3
space
ahmedibrhm Jul 16, 2022
32d1c5e
space
ahmedibrhm Jul 16, 2022
0daa3e8
space
ahmedibrhm Jul 16, 2022
be1a9ad
Merge branch 'main' into issue2
ahmedibrhm Jul 16, 2022
9cb23b8
editing test
ahmedibrhm Jul 16, 2022
7ad93e1
Merge branch 'issue2' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 16, 2022
8ec7067
deprecated
ahmedibrhm Jul 17, 2022
6426ab2
Merge branch 'main' into issue7
ahmedibrhm Jul 17, 2022
7be113a
syntax
ahmedibrhm Jul 17, 2022
c470a4a
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 17, 2022
b78819f
editting existed examples
ahmedibrhm Jul 17, 2022
1090ef8
syntax
ahmedibrhm Jul 17, 2022
c54e4ea
edit past tests
ahmedibrhm Jul 17, 2022
6b25462
editting pivot
ahmedibrhm Jul 17, 2022
58e82ce
Merge branch 'main' into issue7
ahmedibrhm Jul 17, 2022
b73e343
ex
ahmedibrhm Jul 17, 2022
f46c07c
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 17, 2022
3e1aeba
editing internal use
ahmedibrhm Jul 17, 2022
990d3b0
pivot
ahmedibrhm Jul 17, 2022
5dd8315
warning expected
ahmedibrhm Jul 18, 2022
cbfd656
warning
ahmedibrhm Jul 18, 2022
a68c413
ignore doc warning
ahmedibrhm Jul 18, 2022
cc731be
doc
ahmedibrhm Jul 18, 2022
4e4f397
tests
ahmedibrhm Jul 18, 2022
7aa9948
ignore warning
ahmedibrhm Jul 19, 2022
ead96d5
test
ahmedibrhm Jul 19, 2022
8d06022
plotting
ahmedibrhm Jul 19, 2022
8a2e15b
test
ahmedibrhm Jul 19, 2022
ddac985
Merge branch 'main' into issue7
ahmedibrhm Jul 20, 2022
dbbdbf1
doc
ahmedibrhm Jul 20, 2022
11a0928
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 20, 2022
4afe5fa
doc
ahmedibrhm Jul 20, 2022
123f42e
white space
ahmedibrhm Jul 20, 2022
dfa9505
doc
ahmedibrhm Jul 20, 2022
7ccba90
doc
ahmedibrhm Jul 20, 2022
f612f3a
doc
ahmedibrhm Jul 20, 2022
758a88e
doc
ahmedibrhm Jul 20, 2022
2fa0694
stacklevel
ahmedibrhm Jul 20, 2022
e8b2317
pivot
ahmedibrhm Jul 20, 2022
fd68cf8
pivot
ahmedibrhm Jul 20, 2022
1dd52c8
cookbook
ahmedibrhm Jul 20, 2022
917a662
flake8
ahmedibrhm Jul 20, 2022
f02eb6f
flake8
ahmedibrhm Jul 20, 2022
811e63a
what's new
ahmedibrhm Jul 20, 2022
565d80f
syntax
ahmedibrhm Jul 20, 2022
057e642
itr
ahmedibrhm Jul 26, 2022
dcd14d1
car names
ahmedibrhm Jul 26, 2022
fbab0e9
Merge branch 'main' into issue7
ahmedibrhm Jul 26, 2022
6f9cb71
test edit
ahmedibrhm Jul 26, 2022
0845d76
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 26, 2022
acdab83
fixing tests
ahmedibrhm Jul 26, 2022
1d96b70
fixing tests
ahmedibrhm Jul 26, 2022
1ff640c
flake8
ahmedibrhm Jul 26, 2022
da101e4
rst edit
ahmedibrhm Jul 26, 2022
165f53e
__iter__ edit
ahmedibrhm Jul 26, 2022
ef5f767
Merge branch 'main' into issue7
ahmedibrhm Jul 26, 2022
dd9799e
flake8
ahmedibrhm Jul 26, 2022
606d5e4
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 26, 2022
ee1a425
flake8
ahmedibrhm Jul 26, 2022
20d7e64
space
ahmedibrhm Jul 26, 2022
384d1d3
test
ahmedibrhm Jul 27, 2022
e85e0cc
Merge branch 'main' into issue7
ahmedibrhm Jul 27, 2022
e01e409
merge
ahmedibrhm Jul 27, 2022
72d0401
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 27, 2022
9fa72d5
ignore the type
ahmedibrhm Jul 27, 2022
0478afa
mypy
ahmedibrhm Jul 27, 2022
4e6c2fa
Merge branch 'main' into issue7
ahmedibrhm Jul 27, 2022
374fad8
type
ahmedibrhm Jul 27, 2022
5fac1ca
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 27, 2022
6cc3f49
self.keys
ahmedibrhm Jul 28, 2022
5c18c63
tests
ahmedibrhm Jul 28, 2022
d591a2a
.
ahmedibrhm Jul 28, 2022
89922c2
.
ahmedibrhm Jul 28, 2022
bb9ac87
Merge branch 'main' into issue7
ahmedibrhm Jul 28, 2022
4a8c2f7
Merge branch 'main' into issue7
ahmedibrhm Jul 28, 2022
6fa3c17
Merge branch 'main' into issue7
ahmedibrhm Jul 28, 2022
2c97838
Merge branch 'main' into issue7
ahmedibrhm Jul 28, 2022
13d4f35
adding keys
ahmedibrhm Jul 28, 2022
9022ac9
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 28, 2022
cacc726
order
ahmedibrhm Jul 28, 2022
a3bf81a
attribute
ahmedibrhm Jul 29, 2022
4aa9d18
Merge branch 'main' into issue7
ahmedibrhm Jul 29, 2022
3d17829
Merge branch 'main' into issue7
ahmedibrhm Jul 29, 2022
93b1ee1
ignores
ahmedibrhm Jul 29, 2022
a5b22d6
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 29, 2022
797c198
Update hist.py
ahmedibrhm Jul 29, 2022
45eb053
ignore
ahmedibrhm Jul 29, 2022
493f9b6
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 29, 2022
d246058
.
ahmedibrhm Jul 29, 2022
14e5d6a
.
ahmedibrhm Jul 29, 2022
351aa56
.
ahmedibrhm Jul 29, 2022
c971544
Merge branch 'main' into issue7
ahmedibrhm Jul 29, 2022
e1c12e9
.
ahmedibrhm Jul 29, 2022
af71e56
Merge branch 'issue7' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 29, 2022
37843a4
.
ahmedibrhm Jul 29, 2022
5d68f0b
Merge branch 'main' into issue7
ahmedibrhm Jul 30, 2022
1b93ae1
Update doc/source/whatsnew/v1.5.0.rst
ahmedibrhm Aug 1, 2022
ec2656d
Merge branch 'main' into issue7
ahmedibrhm Aug 1, 2022
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/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ Other Deprecations
- 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`)
- Deprecated arguments ``*args`` and ``**kwargs`` in :class:`Rolling`, :class:`Expanding`, and :class:`ExponentialMovingWindow` ops. (:issue:`47836`)
- Deprecated unused arguments ``encoding`` and ``verbose`` in :meth:`Series.to_excel` and :meth:`DataFrame.to_excel` (:issue:`47912`)
- 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`)

.. ---------------------------------------------------------------------------
.. _whatsnew_150.performance:
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,9 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
klass = type(self.obj)

results = []
for name, group in self:
for name, group in self.grouper.get_iterator(
self._selected_obj, axis=self.axis
):
# this setattr is needed for test_transform_lambda_with_datetimetz
object.__setattr__(group, "name", name)
res = func(group, *args, **kwargs)
Expand Down
14 changes: 14 additions & 0 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT], GroupByIndexingMixin):

axis: int
grouper: ops.BaseGrouper
keys: _KeysArgType | None = None
group_keys: bool | lib.NoDefault

@final
Expand Down Expand Up @@ -821,6 +822,19 @@ def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
Generator yielding sequence of (name, subsetted object)
for each group
"""
keys = self.keys
if isinstance(keys, list) and len(keys) == 1:
warnings.warn(
(
"In a future version of pandas, a length 1 "
"tuple will be returned when iterating over a "
"a groupby with a grouper equal to a list of "
"length 1. Don't supply a list with a single grouper "
"to avoid this warning."
),
FutureWarning,
stacklevel=find_stack_level(),
)
return self.grouper.get_iterator(self._selected_obj, axis=self.axis)


Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _groupby_and_merge(by, left: DataFrame, right: DataFrame, merge_pieces):
if all(item in right.columns for item in by):
rby = right.groupby(by, sort=False)

for key, lhs in lby:
for key, lhs in lby.grouper.get_iterator(lby._selected_obj, axis=lby.axis):

if rby is None:
rhs = right
Expand Down
5 changes: 4 additions & 1 deletion pandas/plotting/_matplotlib/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
concat,
)

from pandas.plotting._matplotlib.misc import unpack_single_str_list


def create_iter_data_given_by(
data: DataFrame, kind: str = "hist"
Expand Down Expand Up @@ -108,7 +110,8 @@ def reconstruct_data_with_by(
1 3.0 4.0 NaN NaN
2 NaN NaN 5.0 6.0
"""
grouped = data.groupby(by)
by_modified = unpack_single_str_list(by)
rhshadrach marked this conversation as resolved.
Show resolved Hide resolved
grouped = data.groupby(by_modified)

data_list = []
for key, group in grouped:
Expand Down
4 changes: 3 additions & 1 deletion pandas/plotting/_matplotlib/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
create_iter_data_given_by,
reformat_hist_y_given_by,
)
from pandas.plotting._matplotlib.misc import unpack_single_str_list
from pandas.plotting._matplotlib.tools import (
create_subplots,
flatten_axes,
Expand Down Expand Up @@ -67,7 +68,8 @@ def _args_adjust(self):
# where subplots are created based on by argument
if is_integer(self.bins):
if self.by is not None:
grouped = self.data.groupby(self.by)[self.columns]
by_modified = unpack_single_str_list(self.by)
grouped = self.data.groupby(by_modified)[self.columns]
self.bins = [self._calculate_bins(group) for key, group in grouped]
else:
self.bins = self._calculate_bins(self.data)
Expand Down
8 changes: 8 additions & 0 deletions pandas/plotting/_matplotlib/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,11 @@ def r(h):
ax.legend()
ax.grid()
return ax


def unpack_single_str_list(keys):
# GH 42795
if isinstance(keys, list):
if len(keys) == 1 and isinstance(keys[0], str):
keys = keys[0]
return keys
16 changes: 16 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2795,3 +2795,19 @@ def test_groupby_none_column_name():
result = df.groupby(by=[None]).sum()
expected = DataFrame({"b": [2, 5], "c": [9, 13]}, index=Index([1, 2], name=None))
tm.assert_frame_equal(result, expected)


def test_single_element_list_grouping():
# GH 42795
df = DataFrame(
{"a": [np.nan, 1], "b": [np.nan, 5], "c": [np.nan, 2]}, index=["x", "y"]
)
msg = (
"In a future version of pandas, a length 1 "
"tuple will be returned when iterating over a "
"a groupby with a grouper equal to a list of "
"length 1. Don't supply a list with a single grouper "
"to avoid this warning."
)
with tm.assert_produces_warning(FutureWarning, match=msg):
values, _ = next(iter(df.groupby(["a"])))