BUG SeriesGroupBy.mean() overflowed on some integer arrays (#22487)
State: closed

When integer arrays contained integers that were outside
the range of int64, the conversion would overflow.
Instead only allow allow safe casting and if a safe cast can not
be done, cast to float64 instead.

- [X] closes #22487
- [X] tests added / passed
- [X] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [X] whatsnew entry


Labels:
name: Groupby
name: Algos
description: Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff

Comments:
Hello @troels! Thanks for submitting the PR.

 - There are no PEP8 issues in the file [`pandas/core/groupby/ops.py`](https://github.com/troels/pandas/blob/6796bf4c0fa70ca1223fd225b97663e01e73c81a/pandas/core/groupby/ops.py) !

 - There are no PEP8 issues in the file [`pandas/tests/arrays/test_integer.py`](https://github.com/troels/pandas/blob/6796bf4c0fa70ca1223fd225b97663e01e73c81a/pandas/tests/arrays/test_integer.py) !


# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22653?src=pr&el=h1) Report
> Merging [#22653](https://codecov.io/gh/pandas-dev/pandas/pull/22653?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/0976e1261781153603064eb67d66630a099edcd7?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/22653/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/22653?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #22653      +/-   ##
==========================================
+ Coverage   92.17%   92.17%   +<.01%     
==========================================
  Files         169      169              
  Lines       50708    50711       +3     
==========================================
+ Hits        46740    46743       +3     
  Misses       3968     3968
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.58% <100%> (ø)` | :arrow_up: |
| #single | `42.35% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/22653?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/groupby/ops.py](https://codecov.io/gh/pandas-dev/pandas/pull/22653/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9vcHMucHk=) | `96.81% <100%> (+0.02%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22653?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22653?src=pr&el=footer). Last update [0976e12...5340c0b](https://codecov.io/gh/pandas-dev/pandas/pull/22653?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

lgtm. @troels there might be some other places you can now use this routine (in groupby), but can certainly be done as a followup.

back to you @WillAyd 
Thanks @troels - very nice change!