diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 7fd0ca94e7997..a0ef1d3c71a41 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -143,8 +143,12 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1): new_blocks = [] new_items = [] deleted_items = [] + no_result = object() for block in data.blocks: + # avoid inheriting result from previous step in loop + result = no_result + locs = block.mgr_locs.as_array try: result, _ = self.grouper.aggregate( @@ -171,15 +175,16 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1): except TypeError: # we may have an exception in trying to aggregate # continue and exclude the block - pass + deleted_items.append(locs) + continue finally: + if result is not no_result: + dtype = block.values.dtype - dtype = block.values.dtype - - # see if we can cast the block back to the original dtype - result = block._try_coerce_and_cast_result(result, dtype=dtype) - newb = block.make_block(result) + # see if we can cast the block back to the original dtype + result = block._try_coerce_and_cast_result(result, dtype=dtype) + newb = block.make_block(result) new_items.append(locs) new_blocks.append(newb)