Skip to content

Commit

Permalink
DEPR: Disallow groupby __getitem__ with tuple (#49317)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke authored Oct 26, 2022
1 parent 62757c4 commit 5d9090b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,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`)
- Renamed ``fname`` to ``path`` in :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata` and :meth:`DataFrame.to_feather` (:issue:`30338`)
Expand Down
10 changes: 4 additions & 6 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 5d9090b

Please sign in to comment.