BUG: pivot_table not returning correct type when margin=True and aggfunc='mean' 
State: closed

- [x] closes #24893
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`

# What's new
Use of `maybe_downcast_to_dtype` in `_add_margins` so it can resolve the dtype conversion, avoiding floats being converted to integers when the result of the `aggfunc` is a float.

For the new case, if after applying the aggfunc,  if the margin result is not an integer, the whole column is converted to float:

Example:

```
_df
   A  B  C  D
0  2  1  1  X
1  4  4  3  X
2  6  5  4  Y
3  8  8  6  Y
```

Currently pandas does this:

```
print(pd.pivot_table(_df, index="D", margins=True))                                                              
     A    B  C
D             
X    3  2.5  2
Y    7  6.5  5
All  5  4.5  3

```

with the fix the result is:

```
print(pd.pivot_table(_df, index="D", margins=True))                                                              
     A    B  C
D             
X    3  2.5  2.0
Y    7  6.5  5.0
All  5  4.5  3.5

```

# Issues
There are test referencing that np.means of ints are casted back into ints. However, giving that for the aggregations in the rows, floats are kept when the np.mean of integers is a float, it does not make sense that this behavior does not hold for the margins.

Labels:
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode
name: Dtype Conversions
description: Unexpected or buggy dtype conversions

Comments:
Hello @mabelvj! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:





There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2019-11-23 20:43:22 UTC
@mabelvj Thanks for the PR.

to fix the ci failure you'll need to run `black pandas` https://dev.pandas.io/development/contributing.html#python-pep8-black 

also add test to confirm the fix works.
I see that there are tests referencing that np.means of ints are casted back into ints marked with `pytest.mark.xfail` that are related to this issue, should I update them?:
https://github.com/pandas-dev/pandas/blob/03b3c8fc82b3a18a3ddcad1b3b26d601467fc74c/pandas/tests/reshape/test_pivot.py#L1604
https://github.com/pandas-dev/pandas/blob/03b3c8fc82b3a18a3ddcad1b3b26d601467fc74c/pandas/tests/reshape/test_pivot.py#L1618


> should I update them?

Has the output changed? I would have expected them to fail CI if they were changed to no longer cast from float to int.
@TomAugspurger the results do not change for those, the tests are skipped if failing since they are marked to do so. Still, checking them (since they are related to this issue, because they check the float casting on the margins),  the expected values do not match the output result (old and new pivot_table give the same output here).
this lgtm. @mabelvj can you merge master; ping on green.
ping @mabelvj - if you can fix up merge conflicts can get this one in
@jreback @WillAyd Fixed the conflicts, but Flake8 keeps giving a false positive F841 error on [L1670-1671](https://github.com/mabelvj/pandas/blob/dd121dac85bdbdf5d0552e3692f3f75c498e761c/pandas/tests/reshape/test_pivot.py#L1670-L1671) because of unused variables, but they are used at the last line of the function.

It's strange because other functions have the same structure and do not raise any error.


I think you need to add `tm.assert_result_equal(result, expected)` to the end of the test
@WillAyd Fixed now. It seems the line disappeared when fixing previous conflicts with master.
Great - thanks @mabelvj for seeing this one through!