BUG: df.pivot_table fails when margin is True and only columns is defined
State: closed

- [x] closes #31016 
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Bug
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode

Comments:
Hi, @WillAyd , i think the PR is ok to review now. Please let me know how you plan to do the asv bechmark for this piece, shall I add a test in asv_bench for this one? (as mentioned above, running asv on the current locally is good)
looks good, can you rebase. adding an asv would be nice as well.
Hello @charlesdong1991! 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 2020-01-18 18:21:01 UTC
emm, running asv locally with new tests added, this manipulation does perform much slower than other tests, and the second trial even failed. Any idea/How to interpret why the second one failed? is it because it is too slow and then is above the threshold? @jreback @WillAyd 

I am pretty new to asv, and some feedbacks would be highly appreciated! Thanks! 

<img width="925" alt="Screen Shot 2020-01-18 at 7 35 11 PM" src="https://user-images.githubusercontent.com/9269816/72668741-36c2e080-3a2a-11ea-8eed-c03c48b03685.png">

@charlesdong1991 if you can post the actual shell output instead of a screenshot typically better. That said it doesn’t look like things changed that much. 

Not sure why the second run failed but you can retry with —show-stderr option
```
[ 29.17%] ··· Running (reshape.PivotTable.time_pivot_table--)......
[ 50.00%] · For pandas commit c01d7142 <fix_issue_31016> (round 2/2):
[ 50.00%] ·· Benchmarking virtualenv-py3.7-Cython-matplotlib-numba-numexpr-numpy-odfpy-openpyxl-pytest-scipy-sqlalchemy-tables-xlrd-xlsxwriter-xlwt
[ 54.17%] ··· reshape.PivotTable.time_pivot_table                                                                        25.8±0.5ms
[ 58.33%] ··· reshape.PivotTable.time_pivot_table_agg                                                                      49.5±2ms
[ 62.50%] ··· reshape.PivotTable.time_pivot_table_categorical                                                              19.5±5ms
[ 66.67%] ··· reshape.PivotTable.time_pivot_table_categorical_observed                                                   11.5±0.7ms
[ 70.83%] ··· reshape.PivotTable.time_pivot_table_margins                                                                  94.7±6ms
[ 75.00%] ··· reshape.PivotTable.time_pivot_table_margins_only_column                                                      271±40ms
[ 75.00%] · For pandas commit d170cc05 <multi_pivot^2> (round 2/2):
[ 75.00%] ·· Building for virtualenv-py3.7-Cython-matplotlib-numba-numexpr-numpy-odfpy-openpyxl-pytest-scipy-sqlalchemy-tables-xlrd-xlsxwriter-xlwt...
[ 75.00%] ·· Benchmarking virtualenv-py3.7-Cython-matplotlib-numba-numexpr-numpy-odfpy-openpyxl-pytest-scipy-sqlalchemy-tables-xlrd-xlsxwriter-xlwt
[ 79.17%] ··· reshape.PivotTable.time_pivot_table                                                                          26.3±2ms
[ 83.33%] ··· reshape.PivotTable.time_pivot_table_agg                                                                      52.3±2ms
[ 87.50%] ··· reshape.PivotTable.time_pivot_table_categorical                                                              20.7±3ms
[ 91.67%] ··· reshape.PivotTable.time_pivot_table_categorical_observed                                                   11.5±0.6ms
[ 95.83%] ··· reshape.PivotTable.time_pivot_table_margins                                                                  106±10ms
[100.00%] ··· reshape.PivotTable.time_pivot_table_margins_only_column                                                        failed

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.
```

sorry, is this what you mean by posting actual output? @WillAyd 

Yeah, thanks mate! And seems the first round is running the code in my current PR, and second one is using the code on master branch. `--show-stderr` option does provide some insight, and the reason is this code is not working on the current master, so it failed, see below for the result of our a new run with this option. 

```
[100.00%] ···· Traceback (most recent call last):
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/asv/benchmark.py", line 1434, in <module>
                   main()
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/asv/benchmark.py", line 1427, in main
                   commands[mode](args)
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/asv/benchmark.py", line 1166, in main_run
                   result = benchmark.do_run()
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/asv/benchmark.py", line 573, in do_run
                   return self.run(*self._current_params)
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/asv/benchmark.py", line 672, in run
                   min_run_count=self.min_run_count)
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/asv/benchmark.py", line 704, in benchmark_timing
                   timing = timer.timeit(number)
                 File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/timeit.py", line 176, in timeit
                   timing = self.inner(it, self.timer)
                 File "<timeit-src>", line 6, in inner
                 File "/Users/cw1921/pandas-dev/asv_bench/benchmarks/reshape.py", line 165, in time_pivot_table_margins_only_column
                   self.df.pivot_table(columns=["key2", "key3"], margins=True)
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/pandas/core/frame.py", line 6104, in pivot_table
                   observed=observed,
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/pandas/core/reshape/pivot.py", line 167, in pivot_table
                   fill_value=fill_value,
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/pandas/core/reshape/pivot.py", line 237, in _add_margins
                   margins_name,
                 File "/Users/cw1921/pyvenv/pandas-dev/lib/python3.7/site-packages/pandas/core/reshape/pivot.py", line 353, in _generate_marginal_results
                   table_pieces.append(Series(margin[key], index=[all_key]))
               KeyError: 'one'
               asv: benchmark failed (exit status 1)
```

so i think we are good! @WillAyd @jreback  not sure why the performance even gets slightly better 😅 but at least it's not getting worse, haha

gentle ping @jreback @WillAyd any followup? ^^
lgtm. @WillAyd 
Thanks @charlesdong1991