Skip to content

CLN: dont catch on groupby.mean #28878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,18 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1):
if result is not no_result:
# see if we can cast the block back to the original dtype
result = maybe_downcast_numeric(result, block.dtype)

if result.ndim == 1 and isinstance(result, np.ndarray):
# e.g. block.values was an IntegerArray
try:
# Cast back if feasible
result = type(block.values)._from_sequence(
result, dtype=block.values.dtype
)
except ValueError:
# reshape to be valid for non-Extension Block
result = result.reshape(1, -1)

newb = block.make_block(result)

new_items.append(locs)
Expand Down
13 changes: 3 additions & 10 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,16 +1212,9 @@ def mean(self, *args, **kwargs):
Name: B, dtype: float64
"""
nv.validate_groupby_func("mean", args, kwargs, ["numeric_only"])
try:
return self._cython_agg_general(
"mean", alt=lambda x, axis: Series(x).mean(**kwargs), **kwargs
)
except GroupByError:
raise
except Exception:
with _group_selection_context(self):
f = lambda x: x.mean(axis=self.axis, **kwargs)
return self._python_agg_general(f)
return self._cython_agg_general(
"mean", alt=lambda x, axis: Series(x).mean(**kwargs), **kwargs
)

@Substitution(name="groupby")
@Appender(_common_see_also)
Expand Down