Skip to content

Commit a9afbee

Browse files
committed
revert the changes of the group's behavior
1 parent fd61000 commit a9afbee

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

pandas/core/groupby/grouper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,11 @@ def groups(self) -> dict[Hashable, Index]:
675675
warnings.warn(
676676
"`groups` by one element list returns scalar is deprecated "
677677
"and will be removed. In a future version `groups` by one element "
678-
"list will return tuple.",
678+
"list will return tuple. Use ``df.groupby(by='a').groups`` "
679+
"instead of ``df.groupby(by=['a']).groups`` to avoid this warning",
679680
FutureWarning,
680681
stacklevel=find_stack_level(),
681682
)
682-
cats = [(key,) for key in cats] # type: ignore[assignment]
683683
return self._index.groupby(cats)
684684

685685
@property

pandas/tests/groupby/test_groupby.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,11 +2999,11 @@ def test_groupby_multi_index_codes():
29992999

30003000

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

30053005
df = DataFrame({"x": [10, 20, 30], "y": ["a", "b", "c"]})
3006-
expected = {(10,): [0], (20,): [1], (30,): [2]}
3006+
expected = {10: [0], 20: [1], 30: [2]}
30073007
with tm.assert_produces_warning(FutureWarning, match=msg):
30083008
result = df.groupby(["x"]).groups
30093009
tm.assert_dict_equal(result, expected)

pandas/tests/groupby/test_grouping.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -545,19 +545,62 @@ def test_multiindex_columns_empty_level(self):
545545

546546
df = DataFrame([[1, "A"]], columns=midx)
547547

548+
msg = "`groups` by one element list returns scalar is deprecated"
548549
grouped = df.groupby("to filter").groups
549550
assert grouped["A"] == [0]
550551

551-
msg = "`groups` by one element list returns scalar is deprecated"
552-
553552
with tm.assert_produces_warning(FutureWarning, match=msg):
554553
grouped = df.groupby([("to filter", "")]).groups
555-
assert grouped[("A",)] == [0]
554+
assert grouped["A"] == [0]
556555

557556
df = DataFrame([[1, "A"], [2, "B"]], columns=midx)
558557

558+
expected = df.groupby("to filter").groups
559+
with tm.assert_produces_warning(FutureWarning, match=msg):
560+
result = df.groupby([("to filter", "")]).groups
561+
assert result == expected
562+
563+
df = DataFrame([[1, "A"], [2, "A"]], columns=midx)
564+
565+
expected = df.groupby("to filter").groups
566+
with tm.assert_produces_warning(FutureWarning, match=msg):
567+
result = df.groupby([("to filter", "")]).groups
568+
tm.assert_dict_equal(result, expected)
569+
570+
def test_groupby_multiindex_tuple(self):
571+
# GH 17979, GH#59179
572+
df = DataFrame(
573+
[[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]],
574+
columns=MultiIndex.from_arrays([["a", "b", "b", "c"], [1, 1, 2, 2]]),
575+
)
576+
577+
msg = "`groups` by one element list returns scalar is deprecated"
578+
with tm.assert_produces_warning(FutureWarning, match=msg):
579+
expected = df.groupby([("b", 1)]).groups
580+
result = df.groupby(("b", 1)).groups
581+
tm.assert_dict_equal(expected, result)
582+
583+
df2 = DataFrame(
584+
df.values,
585+
columns=MultiIndex.from_arrays(
586+
[["a", "b", "b", "c"], ["d", "d", "e", "e"]]
587+
),
588+
)
589+
590+
with tm.assert_produces_warning(FutureWarning, match=msg):
591+
expected = df2.groupby([("b", "d")]).groups
592+
result = df.groupby(("b", 1)).groups
593+
tm.assert_dict_equal(expected, result)
594+
595+
df3 = DataFrame(df.values, columns=[("a", "d"), ("b", "d"), ("b", "e"), "c"])
596+
597+
with tm.assert_produces_warning(FutureWarning, match=msg):
598+
expected = df3.groupby([("b", "d")]).groups
599+
result = df.groupby(("b", 1)).groups
600+
tm.assert_dict_equal(expected, result)
601+
559602
def test_groupby_multiindex_partial_indexing_equivalence(self):
560-
# GH 17977
603+
# GH 17977, GH#59179
561604
df = DataFrame(
562605
[[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]],
563606
columns=MultiIndex.from_arrays([["a", "b", "b", "c"], [1, 1, 2, 2]]),
@@ -584,7 +627,6 @@ def test_groupby_multiindex_partial_indexing_equivalence(self):
584627
tm.assert_frame_equal(expected_max, result_max)
585628

586629
msg = "`groups` by one element list returns scalar is deprecated"
587-
588630
with tm.assert_produces_warning(FutureWarning, match=msg):
589631
expected_groups = df.groupby([("a", 1)])[[("b", 1), ("b", 2)]].groups
590632
result_groups = df.groupby([("a", 1)])["b"].groups
@@ -690,16 +732,15 @@ def test_grouping_labels(self, multiindex_dataframe_random_data):
690732
tm.assert_almost_equal(grouped._grouper.codes[0], exp_labels)
691733

692734
def test_list_grouper_with_nat(self):
693-
# GH 14715
735+
# GH 14715, GH#59179
694736
df = DataFrame({"date": date_range("1/1/2011", periods=365, freq="D")})
695737
df.iloc[-1] = pd.NaT
696738
grouper = Grouper(key="date", freq="YS")
697739
msg = "`groups` by one element list returns scalar is deprecated"
698740

699741
# Grouper in a list grouping
700742
result = df.groupby([grouper])
701-
expected = {(Timestamp("2011-01-01"),): list(range(364)), (pd.NaT,): [364]}
702-
743+
expected = {Timestamp("2011-01-01"): Index(list(range(364)))}
703744
with tm.assert_produces_warning(FutureWarning, match=msg):
704745
result = result.groups
705746
tm.assert_dict_equal(result, expected)
@@ -976,7 +1017,7 @@ def test_groups(self, df):
9761017
assert groups is grouped.groups # caching works
9771018

9781019
for k, v in grouped.groups.items():
979-
assert (df.loc[v]["A"] == k[0]).all()
1020+
assert (df.loc[v]["A"] == k).all()
9801021

9811022
grouped = df.groupby(["A", "B"])
9821023
groups = grouped.groups

0 commit comments

Comments
 (0)