diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 8a628dbce3ca7..a10e99c13e548 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -227,6 +227,7 @@ Removal of prior version deprecations/changes - Removed ``pandas.SparseArray`` in favor of :class:`arrays.SparseArray` (:issue:`30642`) - Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame`` (:issue:`30642`) - Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`) +- Enforced disallowing a tuple of column labels into :meth:`.DataFrameGroupBy.__getitem__` (:issue:`30546`) - Removed setting Categorical._codes directly (:issue:`41429`) - Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`) - Enforced the ``display.max_colwidth`` option to not accept negative integers (:issue:`31569`) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 109227633cfdd..4c06ee60d3f6a 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1595,12 +1595,10 @@ def __getitem__(self, key) -> DataFrameGroupBy | SeriesGroupBy: # per GH 23566 if isinstance(key, tuple) and len(key) > 1: # if len == 1, then it becomes a SeriesGroupBy and this is actually - # valid syntax, so don't raise warning - warnings.warn( - "Indexing with multiple keys (implicitly converted to a tuple " - "of keys) will be deprecated, use a list instead.", - FutureWarning, - stacklevel=find_stack_level(), + # valid syntax, so don't raise + raise ValueError( + "Cannot subset columns with a tuple with more than one element. " + "Use a list instead." ) return super().__getitem__(key) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 728575a80f32f..1c8b8e3d33ecf 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -102,13 +102,13 @@ def test_getitem_numeric_column_names(self): tm.assert_frame_equal(result, expected) tm.assert_frame_equal(result2, expected) - # per GH 23566 this should raise a FutureWarning - with tm.assert_produces_warning(FutureWarning): + # per GH 23566 enforced deprecation raises a ValueError + with pytest.raises(ValueError, match="Cannot subset columns with a tuple"): df.groupby(0)[2, 4].mean() - def test_getitem_single_list_of_columns(self, df): - # per GH 23566 this should raise a FutureWarning - with tm.assert_produces_warning(FutureWarning): + def test_getitem_single_tuple_of_columns_raises(self, df): + # per GH 23566 enforced deprecation raises a ValueError + with pytest.raises(ValueError, match="Cannot subset columns with a tuple"): df.groupby("A")["C", "D"].mean() def test_getitem_single_column(self):