Skip to content

Commit e9e8598

Browse files
nbonnottejreback
authored andcommitted
BUG in .groupby for single-row DF, #11741
No KeyError was raised when grouping by a non-existant column Fixes #11741 Xref issue #11640, PR #11717
1 parent 270dac6 commit e9e8598

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

doc/source/whatsnew/v0.18.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ Bug Fixes
531531
of columns didn't match the number of series provided (:issue:`12039`).
532532

533533

534-
534+
- Bug in ``.groupby`` where a ``KeyError`` was not raised for a wrong column if there was only one row in the dataframe (:issue:`11741`)
535535

536536

537537
- Removed ``millisecond`` property of ``DatetimeIndex``. This would always raise a ``ValueError`` (:issue:`12019`).

pandas/core/groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2207,11 +2207,12 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True):
22072207

22082208
if not isinstance(key, (tuple, list)):
22092209
keys = [key]
2210+
match_axis_length = False
22102211
else:
22112212
keys = key
2213+
match_axis_length = len(keys) == len(group_axis)
22122214

22132215
# what are we after, exactly?
2214-
match_axis_length = len(keys) == len(group_axis)
22152216
any_callable = any(callable(g) or isinstance(g, dict) for g in keys)
22162217
any_groupers = any(isinstance(g, Grouper) for g in keys)
22172218
any_arraylike = any(isinstance(g, (list, tuple, Series, Index, np.ndarray))

pandas/tests/test_groupby.py

+7
Original file line numberDiff line numberDiff line change
@@ -3085,6 +3085,13 @@ def test_groupby_keys_same_size_as_index(self):
30853085

30863086
assert_frame_equal(result, expected)
30873087

3088+
def test_groupby_one_row(self):
3089+
# GH 11741
3090+
df1 = pd.DataFrame(np.random.randn(1, 4), columns=list('ABCD'))
3091+
self.assertRaises(KeyError, df1.groupby, 'Z')
3092+
df2 = pd.DataFrame(np.random.randn(2, 4), columns=list('ABCD'))
3093+
self.assertRaises(KeyError, df2.groupby, 'Z')
3094+
30883095
def test_groupby_nat_exclude(self):
30893096
# GH 6992
30903097
df = pd.DataFrame({'values': np.random.randn(8),

0 commit comments

Comments
 (0)