diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index c8ab9599cd92a..11596fe5d4ee3 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -1184,3 +1184,4 @@ Bug Fixes - Bug in ``io.gbq`` when testing for minimum google api client version (:issue:`10652`) - Bug in ``DataFrame`` construction from nested ``dict`` with ``timedelta`` keys (:issue:`11129`) - Bug in ``.fillna`` against may raise ``TypeError`` when data contains datetime dtype (:issue:`7095`, :issue:`11153`) +- Bug in ``.groupby`` when number of keys to group by is same as length of index (:issue:`11185`) diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 9748a594a9b6f..e837445e9e348 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -2111,6 +2111,7 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True): # what are we after, exactly? match_axis_length = len(keys) == len(group_axis) any_callable = any(callable(g) or isinstance(g, dict) for g in keys) + any_groupers = any(isinstance(g, Grouper) for g in keys) any_arraylike = any(isinstance(g, (list, tuple, Series, Index, np.ndarray)) for g in keys) @@ -2123,7 +2124,8 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True): all_in_columns = False if (not any_callable and not all_in_columns - and not any_arraylike and match_axis_length + and not any_arraylike and not any_groupers + and match_axis_length and level is None): keys = [com._asarray_tuplesafe(keys)] diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py index 42d5af587a859..8eb641ce8f494 100644 --- a/pandas/tests/test_groupby.py +++ b/pandas/tests/test_groupby.py @@ -2989,6 +2989,18 @@ def test_groupby_list_infer_array_like(self): result = df.groupby(['foo', 'bar']).mean() expected = df.groupby([df['foo'], df['bar']]).mean()[['val']] + def test_groupby_keys_same_size_as_index(self): + # GH 11185 + freq = 's' + index = pd.date_range(start=np.datetime64( + '2015-09-29T11:34:44-0700'), periods=2, freq=freq) + df = pd.DataFrame([['A', 10], ['B', 15]], columns=[ + 'metric', 'values'], index=index) + result = df.groupby([pd.Grouper(level=0, freq=freq), 'metric']).mean() + expected = df.set_index([df.index, 'metric']) + + assert_frame_equal(result, expected) + def test_groupby_nat_exclude(self): # GH 6992 df = pd.DataFrame({'values': np.random.randn(8),