Skip to content

Commit 5d9090b

Browse files
authored
DEPR: Disallow groupby __getitem__ with tuple (#49317)
1 parent 62757c4 commit 5d9090b

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ Removal of prior version deprecations/changes
228228
- Removed ``pandas.SparseArray`` in favor of :class:`arrays.SparseArray` (:issue:`30642`)
229229
- Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame`` (:issue:`30642`)
230230
- Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)
231+
- Enforced disallowing a tuple of column labels into :meth:`.DataFrameGroupBy.__getitem__` (:issue:`30546`)
231232
- Removed setting Categorical._codes directly (:issue:`41429`)
232233
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)
233234
- Renamed ``fname`` to ``path`` in :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata` and :meth:`DataFrame.to_feather` (:issue:`30338`)

pandas/core/groupby/generic.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1595,12 +1595,10 @@ def __getitem__(self, key) -> DataFrameGroupBy | SeriesGroupBy:
15951595
# per GH 23566
15961596
if isinstance(key, tuple) and len(key) > 1:
15971597
# if len == 1, then it becomes a SeriesGroupBy and this is actually
1598-
# valid syntax, so don't raise warning
1599-
warnings.warn(
1600-
"Indexing with multiple keys (implicitly converted to a tuple "
1601-
"of keys) will be deprecated, use a list instead.",
1602-
FutureWarning,
1603-
stacklevel=find_stack_level(),
1598+
# valid syntax, so don't raise
1599+
raise ValueError(
1600+
"Cannot subset columns with a tuple with more than one element. "
1601+
"Use a list instead."
16041602
)
16051603
return super().__getitem__(key)
16061604

pandas/tests/groupby/test_grouping.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ def test_getitem_numeric_column_names(self):
102102
tm.assert_frame_equal(result, expected)
103103
tm.assert_frame_equal(result2, expected)
104104

105-
# per GH 23566 this should raise a FutureWarning
106-
with tm.assert_produces_warning(FutureWarning):
105+
# per GH 23566 enforced deprecation raises a ValueError
106+
with pytest.raises(ValueError, match="Cannot subset columns with a tuple"):
107107
df.groupby(0)[2, 4].mean()
108108

109-
def test_getitem_single_list_of_columns(self, df):
110-
# per GH 23566 this should raise a FutureWarning
111-
with tm.assert_produces_warning(FutureWarning):
109+
def test_getitem_single_tuple_of_columns_raises(self, df):
110+
# per GH 23566 enforced deprecation raises a ValueError
111+
with pytest.raises(ValueError, match="Cannot subset columns with a tuple"):
112112
df.groupby("A")["C", "D"].mean()
113113

114114
def test_getitem_single_column(self):

0 commit comments

Comments
 (0)