I am trying to compute a simple pivot table with margins. It works fine with margins=False but as soon as i try to set margins to true I get a Key Error. How can I fix this?
Code:
sp_outlook_distr.pivot_table(index=["Type"],
columns=sp_outlook_distr["Date"].dt.year, aggfunc={"Date": np.count_nonzero}, 
fill_value=0, margins=True)
Error:
---------------------------------------------------------------------------
KeyError  Traceback (most recent call last)
<ipython-input-87-fdd674d20a28> in <module>()
      1 # Distribution table - Outlook
----> 2 sp_outlook_distr.pivot_table(index=["Type"], columns=sp_outlook_distr["Date"].dt.year, aggfunc={"Date": np.count_nonzero}, fill_value=0, margins=True)
~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in pivot_table(self, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name)
   5298                            aggfunc=aggfunc, fill_value=fill_value,
   5299                            margins=margins, dropna=dropna,
-> 5300                            margins_name=margins_name)
   5301 
   5302     def stack(self, level=-1, dropna=True):
~/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/pivot.py in pivot_table(data, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name)
    122                              cols=columns, aggfunc=aggfunc,
    123                              observed=dropna,
--> 124                              margins_name=margins_name, fill_value=fill_value)
    125 
    126     # discard the top level
~/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/pivot.py in _add_margins(table, data, values, rows, cols, aggfunc, observed, margins_name, fill_value)
    149             raise ValueError(msg)
    150 
--> 151     grand_margin = _compute_grand_margin(data, values, aggfunc, margins_name)
    152 
    153     # could be passed a Series object with no 'columns'
~/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/pivot.py in _compute_grand_margin(data, values, aggfunc, margins_name)
    219                     grand_margin[k] = getattr(v, aggfunc)()
    220                 elif isinstance(aggfunc, dict):
--> 221                     if isinstance(aggfunc[k], compat.string_types):
    222                         grand_margin[k] = getattr(v, aggfunc[k])()
    223                     else:
KeyError: 'Sovereign'
 
     
    