Skip to content

Commit 19fe376

Browse files
committed
Merge pull request #7089 from cpcloud/groupby-size-windows-bug
BUG: use size attribute (not method call)
2 parents 76dd27f + b722dee commit 19fe376

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

pandas/core/groupby.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ def _last(x):
142142
else:
143143
return _last(x)
144144

145+
146+
def _count_compat(x, axis=0):
147+
return x.size
148+
149+
145150
class Grouper(object):
146151
"""
147152
A Grouper allows the user to specify a groupby instruction for a target object
@@ -721,8 +726,7 @@ def size(self):
721726
numeric_only=False, _convert=True)
722727
last = _groupby_function('last', 'last', _last_compat, numeric_only=False,
723728
_convert=True)
724-
725-
_count = _groupby_function('_count', 'count', lambda x, axis=0: x.size(),
729+
_count = _groupby_function('_count', 'count', _count_compat,
726730
numeric_only=False)
727731

728732
def count(self, axis=0):
@@ -1386,17 +1390,19 @@ def aggregate(self, values, how, axis=0):
13861390
if is_numeric_dtype(values.dtype):
13871391
values = com.ensure_float(values)
13881392
is_numeric = True
1393+
out_dtype = 'f%d' % values.dtype.itemsize
13891394
else:
13901395
is_numeric = issubclass(values.dtype.type, (np.datetime64,
13911396
np.timedelta64))
1397+
out_dtype = 'float64'
13921398
if is_numeric:
13931399
values = values.view('int64')
13941400
else:
13951401
values = values.astype(object)
13961402

13971403
# will be filled in Cython function
1398-
result = np.empty(out_shape,
1399-
dtype=np.dtype('f%d' % values.dtype.itemsize))
1404+
result = np.empty(out_shape, dtype=out_dtype)
1405+
14001406
result.fill(np.nan)
14011407
counts = np.zeros(self.ngroups, dtype=np.int64)
14021408

@@ -1441,7 +1447,6 @@ def _aggregate(self, result, counts, values, how, is_numeric):
14411447
chunk = chunk.squeeze()
14421448
agg_func(result[:, :, i], counts, chunk, comp_ids)
14431449
else:
1444-
#import ipdb; ipdb.set_trace() # XXX BREAKPOINT
14451450
agg_func(result, counts, values, comp_ids)
14461451

14471452
return trans_func(result)

pandas/tests/test_groupby.py

+20
Original file line numberDiff line numberDiff line change
@@ -4214,6 +4214,26 @@ def test_lower_int_prec_count(self):
42144214
name='grp'))
42154215
tm.assert_frame_equal(result, expected)
42164216

4217+
def test_count_uses_size_on_exception(self):
4218+
class RaisingObjectException(Exception):
4219+
pass
4220+
4221+
class RaisingObject(object):
4222+
def __init__(self, msg='I will raise inside Cython'):
4223+
super(RaisingObject, self).__init__()
4224+
self.msg = msg
4225+
4226+
def __eq__(self, other):
4227+
# gets called in Cython to check that raising calls the method
4228+
raise RaisingObjectException(self.msg)
4229+
4230+
df = DataFrame({'a': [RaisingObject() for _ in range(4)],
4231+
'grp': list('ab' * 2)})
4232+
result = df.groupby('grp').count()
4233+
expected = DataFrame({'a': [2, 2]}, index=pd.Index(list('ab'),
4234+
name='grp'))
4235+
tm.assert_frame_equal(result, expected)
4236+
42174237

42184238
def assert_fp_equal(a, b):
42194239
assert (np.abs(a - b) < 1e-12).all()

0 commit comments

Comments
 (0)