Skip to content

Commit f009f97

Browse files
committed
ENH: raise better error when cythonized groupby fails on non-numeric data, re: GH #315
1 parent 2933062 commit f009f97

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pandas/core/groupby.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import pandas._tseries as lib
1414

1515

16+
class GroupByError(Exception):
17+
pass
18+
19+
1620
class GroupBy(object):
1721
"""
1822
Class for grouping and aggregating relational data. See aggregate,
@@ -346,6 +350,9 @@ def _cython_agg_general(self, how):
346350
mask = counts.ravel() > 0
347351
output[name] = result[mask]
348352

353+
if len(output) == 0:
354+
raise GroupByError('No numeric types to aggregate')
355+
349356
return self._wrap_aggregated_output(output, mask)
350357

351358
def _get_multi_index(self, mask):

pandas/tests/test_groupby.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pandas.core.index import Index, MultiIndex
99
from pandas.core.common import rands, groupby
1010
from pandas.core.frame import DataFrame
11+
from pandas.core.groupby import GroupByError
1112
from pandas.core.series import Series
1213
from pandas.util.testing import (assert_panel_equal, assert_frame_equal,
1314
assert_series_equal, assert_almost_equal)
@@ -659,7 +660,6 @@ def _testit(op):
659660
_testit(lambda x: x.sum())
660661
_testit(lambda x: x.mean())
661662

662-
663663
def test_cython_agg_boolean(self):
664664
frame = DataFrame({'a': np.random.randint(0, 5, 50),
665665
'b': np.random.randint(0, 2, 50).astype('bool')})
@@ -668,6 +668,11 @@ def test_cython_agg_boolean(self):
668668

669669
assert_series_equal(result, expected)
670670

671+
def test_cython_agg_nothing_to_agg(self):
672+
frame = DataFrame({'a': np.random.randint(0, 5, 50),
673+
'b': ['foo', 'bar'] * 25})
674+
self.assertRaises(GroupByError, frame.groupby('a')['b'].mean)
675+
671676
def test_grouping_attrs(self):
672677
deleveled = self.mframe.delevel()
673678
grouped = deleveled.groupby(['first', 'second'])

0 commit comments

Comments
 (0)