From 9006019d9ec1c7b4adc5e53df81b43da46a53937 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 4 Jul 2024 10:23:52 +0200 Subject: [PATCH 1/7] deprecate group by one element list get scalar keys --- pandas/core/groupby/grouper.py | 16 ++++++++++++++++ pandas/tests/groupby/test_groupby.py | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 5f680de77649f..db1de636512cb 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -9,12 +9,14 @@ TYPE_CHECKING, final, ) +import warnings import numpy as np from pandas._libs.tslibs import OutOfBoundsDatetime from pandas.errors import InvalidIndexError from pandas.util._decorators import cache_readonly +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( is_list_like, @@ -441,6 +443,7 @@ def __init__( in_axis: bool = False, dropna: bool = True, uniques: ArrayLike | None = None, + key_dtype_str: bool = False, ) -> None: self.level = level self._orig_grouper = grouper @@ -453,6 +456,7 @@ def __init__( self.in_axis = in_axis self._dropna = dropna self._uniques = uniques + self.key_dtype_str = key_dtype_str # we have a single grouper which may be a myriad of things, # some of which are dependent on the passing in level @@ -667,6 +671,15 @@ def groups(self) -> dict[Hashable, Index]: codes, uniques = self._codes_and_uniques uniques = Index._with_infer(uniques, name=self.name) cats = Categorical.from_codes(codes, uniques, validate=False) + if not self.key_dtype_str: + warnings.warn( + "`groups` by one element list returns scalar is deprecated " + "and will be removed. In a future version `groups` by one element " + "list will return tuple.", + FutureWarning, + stacklevel=find_stack_level(), + ) + cats = [(key,) for key in cats] return self._index.groupby(cats) @property @@ -781,7 +794,9 @@ def get_grouper( elif isinstance(key, ops.BaseGrouper): return key, frozenset(), obj + key_dtype_str = False if not isinstance(key, list): + key_dtype_str = True keys = [key] match_axis_length = False else: @@ -892,6 +907,7 @@ def is_in_obj(gpr) -> bool: observed=observed, in_axis=in_axis, dropna=dropna, + key_dtype_str=key_dtype_str, ) if not isinstance(gpr, Grouping) else gpr diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 13fb9cfc4c0e4..9b92b5b460233 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2996,3 +2996,15 @@ def test_groupby_multi_index_codes(): index = df_grouped.index tm.assert_index_equal(index, MultiIndex.from_frame(index.to_frame())) + + +def test_groupby_keys_1length_list(): + # GH#58858 + msg = "`groups` by one element list returns scalar is deprecated" + + df = DataFrame({"x": [10, 20, 30], "y": ["a", "b", "c"]}) + expected = {(10,): [0], (20,): [1], (30,): [2]} + with tm.assert_produces_warning(FutureWarning, match=msg): + result = df.groupby(["x"]).groups + tm.assert_dict_equal(result, expected) + print(result, type(result)) From 1d8edca1afe5fd535ce9186d7b48192feab88df2 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Mon, 8 Jul 2024 16:06:03 +0200 Subject: [PATCH 2/7] fix tests --- pandas/core/groupby/grouper.py | 2 +- pandas/tests/groupby/test_grouping.py | 64 +++++++++------------------ pandas/tests/groupby/test_raises.py | 3 ++ 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index db1de636512cb..506121ad9cb31 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -679,7 +679,7 @@ def groups(self) -> dict[Hashable, Index]: FutureWarning, stacklevel=find_stack_level(), ) - cats = [(key,) for key in cats] + cats = [(key,) for key in cats] # type: ignore[assignment] return self._index.groupby(cats) @property diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 39eadd32f300d..e6cc73ac55288 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -548,45 +548,13 @@ def test_multiindex_columns_empty_level(self): grouped = df.groupby("to filter").groups assert grouped["A"] == [0] - grouped = df.groupby([("to filter", "")]).groups - assert grouped["A"] == [0] - - df = DataFrame([[1, "A"], [2, "B"]], columns=midx) - - expected = df.groupby("to filter").groups - result = df.groupby([("to filter", "")]).groups - assert result == expected - - df = DataFrame([[1, "A"], [2, "A"]], columns=midx) + msg = "`groups` by one element list returns scalar is deprecated" - expected = df.groupby("to filter").groups - result = df.groupby([("to filter", "")]).groups - tm.assert_dict_equal(result, expected) - - def test_groupby_multiindex_tuple(self): - # GH 17979 - df = DataFrame( - [[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]], - columns=MultiIndex.from_arrays([["a", "b", "b", "c"], [1, 1, 2, 2]]), - ) - expected = df.groupby([("b", 1)]).groups - result = df.groupby(("b", 1)).groups - tm.assert_dict_equal(expected, result) - - df2 = DataFrame( - df.values, - columns=MultiIndex.from_arrays( - [["a", "b", "b", "c"], ["d", "d", "e", "e"]] - ), - ) - expected = df2.groupby([("b", "d")]).groups - result = df.groupby(("b", 1)).groups - tm.assert_dict_equal(expected, result) + with tm.assert_produces_warning(FutureWarning, match=msg): + grouped = df.groupby([("to filter", "")]).groups + assert grouped[("A",)] == [0] - df3 = DataFrame(df.values, columns=[("a", "d"), ("b", "d"), ("b", "e"), "c"]) - expected = df3.groupby([("b", "d")]).groups - result = df.groupby(("b", 1)).groups - tm.assert_dict_equal(expected, result) + df = DataFrame([[1, "A"], [2, "B"]], columns=midx) def test_groupby_multiindex_partial_indexing_equivalence(self): # GH 17977 @@ -615,8 +583,11 @@ def test_groupby_multiindex_partial_indexing_equivalence(self): result_max = df.groupby([("a", 1)])["b"].max() tm.assert_frame_equal(expected_max, result_max) - expected_groups = df.groupby([("a", 1)])[[("b", 1), ("b", 2)]].groups - result_groups = df.groupby([("a", 1)])["b"].groups + msg = "`groups` by one element list returns scalar is deprecated" + + with tm.assert_produces_warning(FutureWarning, match=msg): + expected_groups = df.groupby([("a", 1)])[[("b", 1), ("b", 2)]].groups + result_groups = df.groupby([("a", 1)])["b"].groups tm.assert_dict_equal(expected_groups, result_groups) def test_groupby_level(self, sort, multiindex_dataframe_random_data, df): @@ -723,11 +694,15 @@ def test_list_grouper_with_nat(self): df = DataFrame({"date": date_range("1/1/2011", periods=365, freq="D")}) df.iloc[-1] = pd.NaT grouper = Grouper(key="date", freq="YS") + msg = "`groups` by one element list returns scalar is deprecated" # Grouper in a list grouping result = df.groupby([grouper]) - expected = {Timestamp("2011-01-01"): Index(list(range(364)))} - tm.assert_dict_equal(result.groups, expected) + expected = {(Timestamp("2011-01-01"),): list(range(364)), (pd.NaT,): [364]} + + with tm.assert_produces_warning(FutureWarning, match=msg): + result = result.groups + tm.assert_dict_equal(result, expected) # Test case without a list result = df.groupby(grouper) @@ -994,11 +969,14 @@ def test_gb_key_len_equal_axis_len(self): class TestIteration: def test_groups(self, df): grouped = df.groupby(["A"]) - groups = grouped.groups + msg = "`groups` by one element list returns scalar is deprecated" + + with tm.assert_produces_warning(FutureWarning, match=msg): + groups = grouped.groups assert groups is grouped.groups # caching works for k, v in grouped.groups.items(): - assert (df.loc[v]["A"] == k).all() + assert (df.loc[v]["A"] == k[0]).all() grouped = df.groupby(["A", "B"]) groups = grouped.groups diff --git a/pandas/tests/groupby/test_raises.py b/pandas/tests/groupby/test_raises.py index 5a8192a9ffe02..9f3e620ca9872 100644 --- a/pandas/tests/groupby/test_raises.py +++ b/pandas/tests/groupby/test_raises.py @@ -534,6 +534,9 @@ def test_groupby_raises_category_np( _call_and_check(klass, msg, how, gb, groupby_func_np, ()) +@pytest.mark.filterwarnings( + "ignore:`groups` by one element list returns scalar is deprecated" +) @pytest.mark.parametrize("how", ["method", "agg", "transform"]) def test_groupby_raises_category_on_category( how, From 9afb5e5d4c4d7ceda9d3cbf68551446ba7e16b35 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 10 Jul 2024 23:27:22 +0200 Subject: [PATCH 3/7] correct docstring --- pandas/core/groupby/groupby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index bf71bb80b3623..35726db0c0a5a 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -635,7 +635,7 @@ def groups(self) -> dict[Hashable, Index]: 1 1 5 6 2 7 8 9 >>> df.groupby(by=["a"]).groups - {1: [0, 1], 7: [2]} + {(1,): [0, 1], (7,): [2]} For Resampler: From fd610003f598cc4bc603258efb07c58abb2dcac5 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 11 Jul 2024 19:02:44 +0200 Subject: [PATCH 4/7] correct docs --- pandas/core/groupby/groupby.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 35726db0c0a5a..f8d9ee13bd2f3 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -634,8 +634,8 @@ def groups(self) -> dict[Hashable, Index]: 0 1 2 3 1 1 5 6 2 7 8 9 - >>> df.groupby(by=["a"]).groups - {(1,): [0, 1], (7,): [2]} + >>> df.groupby(by="a").groups + {1: [0, 1], 7: [2]} For Resampler: From a9afbee2a684858b81cc338af888a11b9d912595 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 18 Jul 2024 15:41:13 +0200 Subject: [PATCH 5/7] revert the changes of the group's behavior --- pandas/core/groupby/grouper.py | 4 +- pandas/tests/groupby/test_groupby.py | 4 +- pandas/tests/groupby/test_grouping.py | 59 +++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 506121ad9cb31..2879c6106000f 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -675,11 +675,11 @@ def groups(self) -> dict[Hashable, Index]: warnings.warn( "`groups` by one element list returns scalar is deprecated " "and will be removed. In a future version `groups` by one element " - "list will return tuple.", + "list will return tuple. Use ``df.groupby(by='a').groups`` " + "instead of ``df.groupby(by=['a']).groups`` to avoid this warning", FutureWarning, stacklevel=find_stack_level(), ) - cats = [(key,) for key in cats] # type: ignore[assignment] return self._index.groupby(cats) @property diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 9b92b5b460233..ee44726e12239 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2999,11 +2999,11 @@ def test_groupby_multi_index_codes(): def test_groupby_keys_1length_list(): - # GH#58858 + # GH#59179 msg = "`groups` by one element list returns scalar is deprecated" df = DataFrame({"x": [10, 20, 30], "y": ["a", "b", "c"]}) - expected = {(10,): [0], (20,): [1], (30,): [2]} + expected = {10: [0], 20: [1], 30: [2]} with tm.assert_produces_warning(FutureWarning, match=msg): result = df.groupby(["x"]).groups tm.assert_dict_equal(result, expected) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index e6cc73ac55288..7d7288e0074c6 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -545,19 +545,62 @@ def test_multiindex_columns_empty_level(self): df = DataFrame([[1, "A"]], columns=midx) + msg = "`groups` by one element list returns scalar is deprecated" grouped = df.groupby("to filter").groups assert grouped["A"] == [0] - msg = "`groups` by one element list returns scalar is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): grouped = df.groupby([("to filter", "")]).groups - assert grouped[("A",)] == [0] + assert grouped["A"] == [0] df = DataFrame([[1, "A"], [2, "B"]], columns=midx) + expected = df.groupby("to filter").groups + with tm.assert_produces_warning(FutureWarning, match=msg): + result = df.groupby([("to filter", "")]).groups + assert result == expected + + df = DataFrame([[1, "A"], [2, "A"]], columns=midx) + + expected = df.groupby("to filter").groups + with tm.assert_produces_warning(FutureWarning, match=msg): + result = df.groupby([("to filter", "")]).groups + tm.assert_dict_equal(result, expected) + + def test_groupby_multiindex_tuple(self): + # GH 17979, GH#59179 + df = DataFrame( + [[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]], + columns=MultiIndex.from_arrays([["a", "b", "b", "c"], [1, 1, 2, 2]]), + ) + + msg = "`groups` by one element list returns scalar is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + expected = df.groupby([("b", 1)]).groups + result = df.groupby(("b", 1)).groups + tm.assert_dict_equal(expected, result) + + df2 = DataFrame( + df.values, + columns=MultiIndex.from_arrays( + [["a", "b", "b", "c"], ["d", "d", "e", "e"]] + ), + ) + + with tm.assert_produces_warning(FutureWarning, match=msg): + expected = df2.groupby([("b", "d")]).groups + result = df.groupby(("b", 1)).groups + tm.assert_dict_equal(expected, result) + + df3 = DataFrame(df.values, columns=[("a", "d"), ("b", "d"), ("b", "e"), "c"]) + + with tm.assert_produces_warning(FutureWarning, match=msg): + expected = df3.groupby([("b", "d")]).groups + result = df.groupby(("b", 1)).groups + tm.assert_dict_equal(expected, result) + def test_groupby_multiindex_partial_indexing_equivalence(self): - # GH 17977 + # GH 17977, GH#59179 df = DataFrame( [[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]], columns=MultiIndex.from_arrays([["a", "b", "b", "c"], [1, 1, 2, 2]]), @@ -584,7 +627,6 @@ def test_groupby_multiindex_partial_indexing_equivalence(self): tm.assert_frame_equal(expected_max, result_max) msg = "`groups` by one element list returns scalar is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): expected_groups = df.groupby([("a", 1)])[[("b", 1), ("b", 2)]].groups result_groups = df.groupby([("a", 1)])["b"].groups @@ -690,7 +732,7 @@ def test_grouping_labels(self, multiindex_dataframe_random_data): tm.assert_almost_equal(grouped._grouper.codes[0], exp_labels) def test_list_grouper_with_nat(self): - # GH 14715 + # GH 14715, GH#59179 df = DataFrame({"date": date_range("1/1/2011", periods=365, freq="D")}) df.iloc[-1] = pd.NaT grouper = Grouper(key="date", freq="YS") @@ -698,8 +740,7 @@ def test_list_grouper_with_nat(self): # Grouper in a list grouping result = df.groupby([grouper]) - expected = {(Timestamp("2011-01-01"),): list(range(364)), (pd.NaT,): [364]} - + expected = {Timestamp("2011-01-01"): Index(list(range(364)))} with tm.assert_produces_warning(FutureWarning, match=msg): result = result.groups tm.assert_dict_equal(result, expected) @@ -976,7 +1017,7 @@ def test_groups(self, df): assert groups is grouped.groups # caching works for k, v in grouped.groups.items(): - assert (df.loc[v]["A"] == k[0]).all() + assert (df.loc[v]["A"] == k).all() grouped = df.groupby(["A", "B"]) groups = grouped.groups From 7db0ca40d9fdd9b9c3998256ac3995a2bd56086c Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Fri, 19 Jul 2024 13:25:18 +0200 Subject: [PATCH 6/7] move the FutureWarning to GroupBy.groups --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/groupby/groupby.py | 9 +++++++++ pandas/core/groupby/grouper.py | 16 ---------------- pandas/tests/groupby/test_groupby.py | 2 +- pandas/tests/groupby/test_grouping.py | 6 +++--- 5 files changed, 14 insertions(+), 20 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index be4b9c218f9f5..6da063211f095 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -278,6 +278,7 @@ Other Deprecations - 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`) - Deprecated allowing non-keyword arguments in :meth:`Series.to_markdown` except ``buf``. (:issue:`57280`) - Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`) +- 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`) - 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`) - Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`) - 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`) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index f8d9ee13bd2f3..945173bc48fe9 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -654,6 +654,15 @@ def groups(self) -> dict[Hashable, Index]: >>> ser.resample("MS").groups {Timestamp('2023-01-01 00:00:00'): 2, Timestamp('2023-02-01 00:00:00'): 4} """ + if isinstance(self.keys, list) and len(self.keys) == 1: + warnings.warn( + "`groups` by one element list returns scalar is deprecated " + "and will be removed. In a future version `groups` by one element " + "list will return tuple. Use ``df.groupby(by='a').groups`` " + "instead of ``df.groupby(by=['a']).groups`` to avoid this warning", + FutureWarning, + stacklevel=find_stack_level(), + ) return self._grouper.groups @final diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 2879c6106000f..5f680de77649f 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -9,14 +9,12 @@ TYPE_CHECKING, final, ) -import warnings import numpy as np from pandas._libs.tslibs import OutOfBoundsDatetime from pandas.errors import InvalidIndexError from pandas.util._decorators import cache_readonly -from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( is_list_like, @@ -443,7 +441,6 @@ def __init__( in_axis: bool = False, dropna: bool = True, uniques: ArrayLike | None = None, - key_dtype_str: bool = False, ) -> None: self.level = level self._orig_grouper = grouper @@ -456,7 +453,6 @@ def __init__( self.in_axis = in_axis self._dropna = dropna self._uniques = uniques - self.key_dtype_str = key_dtype_str # we have a single grouper which may be a myriad of things, # some of which are dependent on the passing in level @@ -671,15 +667,6 @@ def groups(self) -> dict[Hashable, Index]: codes, uniques = self._codes_and_uniques uniques = Index._with_infer(uniques, name=self.name) cats = Categorical.from_codes(codes, uniques, validate=False) - if not self.key_dtype_str: - warnings.warn( - "`groups` by one element list returns scalar is deprecated " - "and will be removed. In a future version `groups` by one element " - "list will return tuple. Use ``df.groupby(by='a').groups`` " - "instead of ``df.groupby(by=['a']).groups`` to avoid this warning", - FutureWarning, - stacklevel=find_stack_level(), - ) return self._index.groupby(cats) @property @@ -794,9 +781,7 @@ def get_grouper( elif isinstance(key, ops.BaseGrouper): return key, frozenset(), obj - key_dtype_str = False if not isinstance(key, list): - key_dtype_str = True keys = [key] match_axis_length = False else: @@ -907,7 +892,6 @@ def is_in_obj(gpr) -> bool: observed=observed, in_axis=in_axis, dropna=dropna, - key_dtype_str=key_dtype_str, ) if not isinstance(gpr, Grouping) else gpr diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index ee44726e12239..5b84057ba6ce8 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2999,7 +2999,7 @@ def test_groupby_multi_index_codes(): def test_groupby_keys_1length_list(): - # GH#59179 + # GH#58858 msg = "`groups` by one element list returns scalar is deprecated" df = DataFrame({"x": [10, 20, 30], "y": ["a", "b", "c"]}) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 7d7288e0074c6..28c82a9194bc6 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -1014,16 +1014,16 @@ def test_groups(self, df): with tm.assert_produces_warning(FutureWarning, match=msg): groups = grouped.groups - assert groups is grouped.groups # caching works + assert groups is grouped.groups # caching works - for k, v in grouped.groups.items(): + for k, v in groups.items(): assert (df.loc[v]["A"] == k).all() grouped = df.groupby(["A", "B"]) groups = grouped.groups assert groups is grouped.groups # caching works - for k, v in grouped.groups.items(): + for k, v in groups.items(): assert (df.loc[v]["A"] == k[0]).all() assert (df.loc[v]["B"] == k[1]).all() From fe3514ad6e1cc971ff996ba9b732294264fd0050 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Sun, 21 Jul 2024 20:28:53 +0200 Subject: [PATCH 7/7] remove unnecessary test, rename a variable --- pandas/tests/groupby/test_groupby.py | 12 ------------ pandas/tests/groupby/test_grouping.py | 4 ++-- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 5b84057ba6ce8..13fb9cfc4c0e4 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2996,15 +2996,3 @@ def test_groupby_multi_index_codes(): index = df_grouped.index tm.assert_index_equal(index, MultiIndex.from_frame(index.to_frame())) - - -def test_groupby_keys_1length_list(): - # GH#58858 - msg = "`groups` by one element list returns scalar is deprecated" - - df = DataFrame({"x": [10, 20, 30], "y": ["a", "b", "c"]}) - expected = {10: [0], 20: [1], 30: [2]} - with tm.assert_produces_warning(FutureWarning, match=msg): - result = df.groupby(["x"]).groups - tm.assert_dict_equal(result, expected) - print(result, type(result)) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 28c82a9194bc6..814b35ad577f1 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -739,10 +739,10 @@ def test_list_grouper_with_nat(self): msg = "`groups` by one element list returns scalar is deprecated" # Grouper in a list grouping - result = df.groupby([grouper]) + gb = df.groupby([grouper]) expected = {Timestamp("2011-01-01"): Index(list(range(364)))} with tm.assert_produces_warning(FutureWarning, match=msg): - result = result.groups + result = gb.groups tm.assert_dict_equal(result, expected) # Test case without a list