Skip to content

Commit 568c1ad

Browse files
BUG: DataFrame GroupBy indexing with single items DeprecationWarning(pandas-dev#23566)
1 parent e817fff commit 568c1ad

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

pandas/core/groupby/generic.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Union,
2525
cast,
2626
)
27+
import warnings
2728

2829
import numpy as np
2930

@@ -325,7 +326,7 @@ def _aggregate_multiple_funcs(self, arg):
325326
return DataFrame(results, columns=columns)
326327

327328
def _wrap_series_output(
328-
self, output: Mapping[base.OutputKey, Union[Series, np.ndarray]], index: Index,
329+
self, output: Mapping[base.OutputKey, Union[Series, np.ndarray]], index: Index
329330
) -> Union[Series, DataFrame]:
330331
"""
331332
Wraps the output of a SeriesGroupBy operation into the expected result.
@@ -1574,6 +1575,17 @@ def filter(self, func, dropna=True, *args, **kwargs):
15741575

15751576
return self._apply_filter(indices, dropna)
15761577

1578+
def __getitem__(self, key):
1579+
# per GH 23566
1580+
if isinstance(key, tuple):
1581+
warnings.warn(
1582+
"Indexing with individual keys or with a tuple of keys "
1583+
"will be deprecated, use a list instead.",
1584+
DeprecationWarning,
1585+
stacklevel=2,
1586+
)
1587+
return super().__getitem__(key)
1588+
15771589
def _gotitem(self, key, ndim: int, subset=None):
15781590
"""
15791591
sub-classes to define

pandas/tests/groupby/test_grouping.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ def test_getitem_list_of_columns(self):
7171
)
7272

7373
result = df.groupby("A")[["C", "D"]].mean()
74-
result2 = df.groupby("A")["C", "D"].mean()
75-
result3 = df.groupby("A")[df.columns[2:4]].mean()
74+
result2 = df.groupby("A")[df.columns[2:4]].mean()
7675

7776
expected = df.loc[:, ["A", "C", "D"]].groupby("A").mean()
7877

7978
tm.assert_frame_equal(result, expected)
8079
tm.assert_frame_equal(result2, expected)
81-
tm.assert_frame_equal(result3, expected)
80+
81+
# per GH 23566 this should raise a deprecation warning
82+
with tm.assert_produces_warning(DeprecationWarning):
83+
df.groupby("A")["C", "D"].mean()
8284

8385
def test_getitem_numeric_column_names(self):
8486
# GH #13731

0 commit comments

Comments
 (0)