Skip to content

DEPR: Disallow groupby __getitem__ with tuple #49317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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`)
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