Skip to content

Commit 9006019

Browse files
committed
deprecate group by one element list get scalar keys
1 parent dcb5494 commit 9006019

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pandas/core/groupby/grouper.py

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

1314
import numpy as np
1415

1516
from pandas._libs.tslibs import OutOfBoundsDatetime
1617
from pandas.errors import InvalidIndexError
1718
from pandas.util._decorators import cache_readonly
19+
from pandas.util._exceptions import find_stack_level
1820

1921
from pandas.core.dtypes.common import (
2022
is_list_like,
@@ -441,6 +443,7 @@ def __init__(
441443
in_axis: bool = False,
442444
dropna: bool = True,
443445
uniques: ArrayLike | None = None,
446+
key_dtype_str: bool = False,
444447
) -> None:
445448
self.level = level
446449
self._orig_grouper = grouper
@@ -453,6 +456,7 @@ def __init__(
453456
self.in_axis = in_axis
454457
self._dropna = dropna
455458
self._uniques = uniques
459+
self.key_dtype_str = key_dtype_str
456460

457461
# we have a single grouper which may be a myriad of things,
458462
# some of which are dependent on the passing in level
@@ -667,6 +671,15 @@ def groups(self) -> dict[Hashable, Index]:
667671
codes, uniques = self._codes_and_uniques
668672
uniques = Index._with_infer(uniques, name=self.name)
669673
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.",
679+
FutureWarning,
680+
stacklevel=find_stack_level(),
681+
)
682+
cats = [(key,) for key in cats]
670683
return self._index.groupby(cats)
671684

672685
@property
@@ -781,7 +794,9 @@ def get_grouper(
781794
elif isinstance(key, ops.BaseGrouper):
782795
return key, frozenset(), obj
783796

797+
key_dtype_str = False
784798
if not isinstance(key, list):
799+
key_dtype_str = True
785800
keys = [key]
786801
match_axis_length = False
787802
else:
@@ -892,6 +907,7 @@ def is_in_obj(gpr) -> bool:
892907
observed=observed,
893908
in_axis=in_axis,
894909
dropna=dropna,
910+
key_dtype_str=key_dtype_str,
895911
)
896912
if not isinstance(gpr, Grouping)
897913
else gpr

pandas/tests/groupby/test_groupby.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,3 +2996,15 @@ def test_groupby_multi_index_codes():
29962996

29972997
index = df_grouped.index
29982998
tm.assert_index_equal(index, MultiIndex.from_frame(index.to_frame()))
2999+
3000+
3001+
def test_groupby_keys_1length_list():
3002+
# GH#58858
3003+
msg = "`groups` by one element list returns scalar is deprecated"
3004+
3005+
df = DataFrame({"x": [10, 20, 30], "y": ["a", "b", "c"]})
3006+
expected = {(10,): [0], (20,): [1], (30,): [2]}
3007+
with tm.assert_produces_warning(FutureWarning, match=msg):
3008+
result = df.groupby(["x"]).groups
3009+
tm.assert_dict_equal(result, expected)
3010+
print(result, type(result))

0 commit comments

Comments
 (0)