title: How to convert Pandas MultiIndex/PeriodIndex to Dict while keeping values and periods separate
body: <p>How can I complete the conversion of a pandas multi-indexed dataframe into the appropriate dict output. Example, attempt, and intended output below.</p>
  
  <p>Example data:</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  
  # Setup Data/Define Helper func for Dict -&gt; Multi-Index DF
  
  def dict_to_multiDF_format(dictionary: dict) -&gt; Dict:
      return {(outerKey, innerKey): values for outerKey, innerDict in dictionary.items() for innerKey, values in
             innerDict.items()}
  
  d = {
      'x': {'y': np.random.rand(4), 'z': np.random.randn(4)},
      'aa': {'y': np.random.rand(4), 'z': np.random.randn(4)}
      }
  
  # Create DF w/ PeriodIndex
  df = pd.DataFrame(dict_to_multiDF_format(d))
  df.index = pd.PeriodIndex(start='01-2018', end='04-2018', freq='M')
  </code></pre>
  
  <p>Now that the DF is created, I'd like to be able to get it back out to a multi-level dict format. I modified the following from a different SO answer which brings me almost there:</p>
  
  <pre><code>def multiDF_to_dict(df: pd.DataFrame) -&gt; dict:
      return df.T.groupby(level=0).apply(lambda df: df.xs(df.name).to_dict('index')).to_dict()
  </code></pre>
  
  <p>At this point, the resulting dict type mapping would be Dict[str, Dict[str, Dict[pd.Period, float]]]. What I'd like to be able to return is Dict[str, Dict[str, List[float]]] where, in example above, I could call:</p>
  
  <pre><code>output = multiDF_to_dict(df)
  
  # the following to be True
  isinstance(output['x']['z'], list)
  isinstance(output['x']['z'][0], float)
  
  isinstance(output['periods'], list)
  isinstance(output['periods'][0], pd.Period)
  </code></pre>
  
  <p>Note that the pd.Period has been moved as an outer dict item as it applies across all values.</p>
  
  <p>Edit: To better explain the output, the output dict above returns:</p>
  
  <pre><code>In[163]: output['x']['z']
  Out[163]: 
  {Period('2018-01', 'M'): 1.0308097883976446,
   Period('2018-02', 'M'): 0.6475015766242127,
   Period('2018-03', 'M'): 0.6703669301328639,
   Period('2018-04', 'M'): -0.9079304895787961}
  </code></pre>
  
  <p>I'd like the same call to return:</p>
  
  <pre><code>[1.0308097883976446, 0.6475015766242127, 0.6703669301328639,-0.9079304895787961]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>In[164]: output['periods'] # from PeriodIndex
  Out[164]: ['2018-01', '2018-02', '2018-03', '2018-04']
  </code></pre>
  
  <p>Thanks!</p>