Reindexing a multiindex in pandas dataframe
<p>I'm trying to reindex a 2-level multiindex pandas dataframe. Data struct looks like this:</p>

<pre><code>In [1]: df.head(5)


Out [1]:                                arrivals    departs
station         datetime
S1              2014-03-03 07:45:00     1           1
                2014-03-03 09:00:00     2           1
                2014-03-03 11:45:00     1           1
                2014-03-04 08:45:00     1           1
                2014-03-04 09:45:00     2           1
</code></pre>

<p>I want to fill <code>datetime</code> gaps with 15 minute intervals, but when I call</p>

<pre><code>In [2]: df.reindex(pd.date_range(start='2014-03-03 07:45:00', 
                   end='2014-03-04 07:45:00', freq='15min'), level=1)
</code></pre>

<p>I get the exact same dataframe. I expected something like the following</p>

<pre><code>Out [2]:                                arrivals    departs
station         datetime
S1              2014-03-03 07:45:00     1           1 &lt;-- original row
                2014-03-03 08:00:00     0           0 &lt;-- filled in row
                2014-03-03 08:15:00     0           0 &lt;-- filled in
                2014-03-03 08:30:00     0           0 &lt;-- filled in
                2014-03-03 08:45:00     0           0 &lt;-- filled in
                2014-03-03 09:00:00     2           1 &lt;-- original
                etc...
</code></pre>

<p>Any ideas?</p>

<p>Turn it back into a simple datetimeindex and fill the gaps:</p>

<pre><code>df = (df.unstack(level=0)
        .reindex(pd.date_range(start='2014-03-03 07:45:00', 
                   end='2014-03-04 07:45:00', freq='15min')))


df = df.fillna(0)  # for the data, 0 is the desired value

df.stack('station').swaplevel(0,1).sort_index()
</code></pre>

python pandas multi-index