9
9
TYPE_CHECKING ,
10
10
final ,
11
11
)
12
+ import warnings
12
13
13
14
import numpy as np
14
15
15
16
from pandas ._libs .tslibs import OutOfBoundsDatetime
16
17
from pandas .errors import InvalidIndexError
17
18
from pandas .util ._decorators import cache_readonly
19
+ from pandas .util ._exceptions import find_stack_level
18
20
19
21
from pandas .core .dtypes .common import (
20
22
is_list_like ,
@@ -441,6 +443,7 @@ def __init__(
441
443
in_axis : bool = False ,
442
444
dropna : bool = True ,
443
445
uniques : ArrayLike | None = None ,
446
+ key_dtype_str : bool = False ,
444
447
) -> None :
445
448
self .level = level
446
449
self ._orig_grouper = grouper
@@ -453,6 +456,7 @@ def __init__(
453
456
self .in_axis = in_axis
454
457
self ._dropna = dropna
455
458
self ._uniques = uniques
459
+ self .key_dtype_str = key_dtype_str
456
460
457
461
# we have a single grouper which may be a myriad of things,
458
462
# some of which are dependent on the passing in level
@@ -667,6 +671,15 @@ def groups(self) -> dict[Hashable, Index]:
667
671
codes , uniques = self ._codes_and_uniques
668
672
uniques = Index ._with_infer (uniques , name = self .name )
669
673
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 ]
670
683
return self ._index .groupby (cats )
671
684
672
685
@property
@@ -781,7 +794,9 @@ def get_grouper(
781
794
elif isinstance (key , ops .BaseGrouper ):
782
795
return key , frozenset (), obj
783
796
797
+ key_dtype_str = False
784
798
if not isinstance (key , list ):
799
+ key_dtype_str = True
785
800
keys = [key ]
786
801
match_axis_length = False
787
802
else :
@@ -892,6 +907,7 @@ def is_in_obj(gpr) -> bool:
892
907
observed = observed ,
893
908
in_axis = in_axis ,
894
909
dropna = dropna ,
910
+ key_dtype_str = key_dtype_str ,
895
911
)
896
912
if not isinstance (gpr , Grouping )
897
913
else gpr
0 commit comments