Skip to content

Commit

Permalink
ENH: raise better error when cythonized groupby fails on non-numeric …
Browse files Browse the repository at this point in the history
…data, re: GH #315
  • Loading branch information
wesm committed Nov 3, 2011
1 parent 2933062 commit f009f97
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import pandas._tseries as lib


class GroupByError(Exception):
pass


class GroupBy(object):
"""
Class for grouping and aggregating relational data. See aggregate,
Expand Down Expand Up @@ -346,6 +350,9 @@ def _cython_agg_general(self, how):
mask = counts.ravel() > 0
output[name] = result[mask]

if len(output) == 0:
raise GroupByError('No numeric types to aggregate')

return self._wrap_aggregated_output(output, mask)

def _get_multi_index(self, mask):
Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pandas.core.index import Index, MultiIndex
from pandas.core.common import rands, groupby
from pandas.core.frame import DataFrame
from pandas.core.groupby import GroupByError
from pandas.core.series import Series
from pandas.util.testing import (assert_panel_equal, assert_frame_equal,
assert_series_equal, assert_almost_equal)
Expand Down Expand Up @@ -659,7 +660,6 @@ def _testit(op):
_testit(lambda x: x.sum())
_testit(lambda x: x.mean())


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

assert_series_equal(result, expected)

def test_cython_agg_nothing_to_agg(self):
frame = DataFrame({'a': np.random.randint(0, 5, 50),
'b': ['foo', 'bar'] * 25})
self.assertRaises(GroupByError, frame.groupby('a')['b'].mean)

def test_grouping_attrs(self):
deleveled = self.mframe.delevel()
grouped = deleveled.groupby(['first', 'second'])
Expand Down

0 comments on commit f009f97

Please sign in to comment.