Skip to content

Commit 7db0ca4

Browse files
committed
move the FutureWarning to GroupBy.groups
1 parent a9afbee commit 7db0ca4

File tree

5 files changed

+14
-20
lines changed

5 files changed

+14
-20
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ Other Deprecations
278278
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.all`, :meth:`DataFrame.min`, :meth:`DataFrame.max`, :meth:`DataFrame.sum`, :meth:`DataFrame.prod`, :meth:`DataFrame.mean`, :meth:`DataFrame.median`, :meth:`DataFrame.sem`, :meth:`DataFrame.var`, :meth:`DataFrame.std`, :meth:`DataFrame.skew`, :meth:`DataFrame.kurt`, :meth:`Series.all`, :meth:`Series.min`, :meth:`Series.max`, :meth:`Series.sum`, :meth:`Series.prod`, :meth:`Series.mean`, :meth:`Series.median`, :meth:`Series.sem`, :meth:`Series.var`, :meth:`Series.std`, :meth:`Series.skew`, and :meth:`Series.kurt`. (:issue:`57087`)
279279
- Deprecated allowing non-keyword arguments in :meth:`Series.to_markdown` except ``buf``. (:issue:`57280`)
280280
- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`)
281+
- Deprecated behavior of :meth:`.DataFrameGroupBy.groups` and :meth:`.SeriesGroupBy.groups`, in a future version ``groups`` by one element list will return tuple instead of scalar. (:issue:`58858`)
281282
- Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``datetime.timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties. (:issue:`57463`)
282283
- Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`)
283284
- Deprecated strings ``w``, ``d``, ``MIN``, ``MS``, ``US`` and ``NS`` denoting units in :class:`Timedelta` in favour of ``W``, ``D``, ``min``, ``ms``, ``us`` and ``ns`` (:issue:`59051`)

pandas/core/groupby/groupby.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,15 @@ def groups(self) -> dict[Hashable, Index]:
654654
>>> ser.resample("MS").groups
655655
{Timestamp('2023-01-01 00:00:00'): 2, Timestamp('2023-02-01 00:00:00'): 4}
656656
"""
657+
if isinstance(self.keys, list) and len(self.keys) == 1:
658+
warnings.warn(
659+
"`groups` by one element list returns scalar is deprecated "
660+
"and will be removed. In a future version `groups` by one element "
661+
"list will return tuple. Use ``df.groupby(by='a').groups`` "
662+
"instead of ``df.groupby(by=['a']).groups`` to avoid this warning",
663+
FutureWarning,
664+
stacklevel=find_stack_level(),
665+
)
657666
return self._grouper.groups
658667

659668
@final

pandas/core/groupby/grouper.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
TYPE_CHECKING,
1010
final,
1111
)
12-
import warnings
1312

1413
import numpy as np
1514

1615
from pandas._libs.tslibs import OutOfBoundsDatetime
1716
from pandas.errors import InvalidIndexError
1817
from pandas.util._decorators import cache_readonly
19-
from pandas.util._exceptions import find_stack_level
2018

2119
from pandas.core.dtypes.common import (
2220
is_list_like,
@@ -443,7 +441,6 @@ def __init__(
443441
in_axis: bool = False,
444442
dropna: bool = True,
445443
uniques: ArrayLike | None = None,
446-
key_dtype_str: bool = False,
447444
) -> None:
448445
self.level = level
449446
self._orig_grouper = grouper
@@ -456,7 +453,6 @@ def __init__(
456453
self.in_axis = in_axis
457454
self._dropna = dropna
458455
self._uniques = uniques
459-
self.key_dtype_str = key_dtype_str
460456

461457
# we have a single grouper which may be a myriad of things,
462458
# some of which are dependent on the passing in level
@@ -671,15 +667,6 @@ def groups(self) -> dict[Hashable, Index]:
671667
codes, uniques = self._codes_and_uniques
672668
uniques = Index._with_infer(uniques, name=self.name)
673669
cats = Categorical.from_codes(codes, uniques, validate=False)
674-
if not self.key_dtype_str:
675-
warnings.warn(
676-
"`groups` by one element list returns scalar is deprecated "
677-
"and will be removed. In a future version `groups` by one element "
678-
"list will return tuple. Use ``df.groupby(by='a').groups`` "
679-
"instead of ``df.groupby(by=['a']).groups`` to avoid this warning",
680-
FutureWarning,
681-
stacklevel=find_stack_level(),
682-
)
683670
return self._index.groupby(cats)
684671

685672
@property
@@ -794,9 +781,7 @@ def get_grouper(
794781
elif isinstance(key, ops.BaseGrouper):
795782
return key, frozenset(), obj
796783

797-
key_dtype_str = False
798784
if not isinstance(key, list):
799-
key_dtype_str = True
800785
keys = [key]
801786
match_axis_length = False
802787
else:
@@ -907,7 +892,6 @@ def is_in_obj(gpr) -> bool:
907892
observed=observed,
908893
in_axis=in_axis,
909894
dropna=dropna,
910-
key_dtype_str=key_dtype_str,
911895
)
912896
if not isinstance(gpr, Grouping)
913897
else gpr

pandas/tests/groupby/test_groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2999,7 +2999,7 @@ def test_groupby_multi_index_codes():
29992999

30003000

30013001
def test_groupby_keys_1length_list():
3002-
# GH#59179
3002+
# GH#58858
30033003
msg = "`groups` by one element list returns scalar is deprecated"
30043004

30053005
df = DataFrame({"x": [10, 20, 30], "y": ["a", "b", "c"]})

pandas/tests/groupby/test_grouping.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,16 +1014,16 @@ def test_groups(self, df):
10141014

10151015
with tm.assert_produces_warning(FutureWarning, match=msg):
10161016
groups = grouped.groups
1017-
assert groups is grouped.groups # caching works
1017+
assert groups is grouped.groups # caching works
10181018

1019-
for k, v in grouped.groups.items():
1019+
for k, v in groups.items():
10201020
assert (df.loc[v]["A"] == k).all()
10211021

10221022
grouped = df.groupby(["A", "B"])
10231023
groups = grouped.groups
10241024
assert groups is grouped.groups # caching works
10251025

1026-
for k, v in grouped.groups.items():
1026+
for k, v in groups.items():
10271027
assert (df.loc[v]["A"] == k[0]).all()
10281028
assert (df.loc[v]["B"] == k[1]).all()
10291029

0 commit comments

Comments
 (0)