BUG: make `mean()` raise an exception for strings
State: closed

- [x] closes #44008 
- [x] closes #36703
- [x] closes #34671
- [x] closes #22642 
- [x] closes #26927 
- [x] closes #13916
- [x] tests added / passed
- [x] Ensure all linting tests pass, see [here](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit) for how to run them
- [x] whatsnew entry

This PR makes `mean()` raise an exception when `sum()` returns a string. Sum performs as expected since it concatenates. However `_ensure_numeric` causes implicit conversion of `str` to a numeric datatype
https://github.com/pandas-dev/pandas/blob/f3f90c33966e3ba334c459ad89d607f820caa1f8/pandas/core/nanops.py#L694
Instead of this, the return value of `sum()` will be checked and if it is a string, an exception will be raised. 
This  is a breaking change.

Labels:
name: Algos
description: Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff
name: ExtensionArray
description: Extending pandas with custom dtypes or arrays.

Comments:
There are a bunch of issues about reductions with strings.  can you see if this closes any of the others?
Added all the issues I could find that this PR would close, I think we also need to decide whether we want to keep the behavior of `sum()` concatenating strings, add a note about the same or change it.
Regarding the failing tests, I'll try to see why they are failing and try to fix that.
Regarding the failing tests (reporting from gitter here): I also can't reproduce those locally when checking out this branch. Not fully sure what would cause this discrepancy (in any case it doesn't seem to be related to the python or numpy version, since CI is failing for several versions of those).
@phofl I reran the tests after installing the environments from ci using `conda` and the tests pass locally even on the environments (variants of `actions-38.yaml`) for which the tests are failing on ci. Any idea why this is happening?
ok if you are going to close an issue, pls explicitly make sure its listed in the test its (e.g. a reference) or have an explict test.
dont' worry about .sum it is fine.
Not really, no
@kinshukdua can you add tests specific to each of the issues this is closing
@jbrockmendel yes, I'll add them, sorry for the delay.
can you replicate these examples in tests: 
https://github.com/pandas-dev/pandas/issues/22642
https://github.com/pandas-dev/pandas/issues/36703
https://github.com/pandas-dev/pandas/issues/44008

also pls list the issues in the whatsnew note
> can you replicate these examples in tests: #22642 #36703 #44008
> 
> also pls list the issues in the whatsnew note

The column `"A"` tests the string to integer implicit conversion in #22642 and #44008, since the column won't be dropped if they are integers.

The column `"D"` in the test dataframe takes care of the cascading to complex-128 issue in #36703. If `J` is converted to complex, we would see that in the output series.

Should I create a different test for each issue?

can you merge master and will have a look
@jreback, sorry for the delay, I rebased to fix the merge conflict.
@kinshukdua this is failing tests
this is worthile but needs to be passing, and pls merge master
@jreback as I've explained before, for some reason the tests are all passing on my local environment, I've tried to reproduce the CI as close as possible by using the yaml files to recreate my testing environment. However, the tests still seem to pass. @jorisvandenbossche confirmed it by doing the same . I'm not sure what I should do.
> @jreback as I've explained before, for some reason the tests are all passing on my local environment, I've tried to reproduce the CI as close as possible by using the yaml files to recreate my testing environment. However, the tests still seem to pass. @jorisvandenbossche confirmed it by doing the same . I'm not sure what I should do.

well sure, we are running multiple environments in CI which may have slightly different behavior.

you need to install one of the failing environments and investigate.

we cannot merge until CI is green.
FWIW i wasn't able to reproduce the errors on either my "normal" build or a py38+np1.18.5 build.  @kinshukdua is right that something _weird_ is going on here.

Doesn't change the fact that the CI needs to get to green before this can be merged.  Just saying it's not an easy problem.
@kinshukdua if you can merge master and get this passing
Thanks for the effort on this pull request, but it appears to have gone stale. If interested in continuing, please merge in main and we can reopen.