Insert missing weekdays in pandas dataframe and fill them with NaN
<p>I am trying to insert missing weekdays in a time series dataframe such has </p>

<pre><code>import pandas as pd
from pandas.tseries.offsets import *
df = pd.DataFrame([['2016-09-30', 10, 2020], ['2016-10-03', 20, 2424], ['2016-10-05', 5, 232]], columns=['date', 'price', 'vol']).set_index('date')
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
</code></pre>

<p>data looks like this :</p>

<pre><code>Out[300]: 
            price   vol
date                   
2016-09-30     10  2020
2016-10-03     20  2424
2016-10-05      5   232
</code></pre>

<p>I can create a series of week days easily with <code>pd.date_range()</code></p>

<pre><code>pd.date_range('2016-09-30', '2016-10-05', freq=BDay())
Out[301]: DatetimeIndex(['2016-09-30', '2016-10-03', '2016-10-04', '2016-10-05'], dtype='datetime64[ns]', freq='B')
</code></pre>

<p>based on that DateTimeIndex I would like to add missing dates in my <code>df</code>and fill column values with NaN so I get:</p>

<pre><code>Out[300]: 
            price   vol
date                   
2016-09-30     10  2020
2016-10-03     20  2424
2016-10-04     NaN  NaN
2016-10-05      5   232
</code></pre>

<p>is there an easy way to do this? Thanks!</p>

<p>Alternatively, you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html" rel="nofollow">pandas.DataFrame.resample()</a>, specifying 'B' for <em>Business Day</em> with no need to specify beginning or end date sequence as along as the dataframe maintains a datetime index</p>

<pre><code>df = df.resample('B').sum()

#             price     vol
# date                     
# 2016-09-30   10.0  2020.0
# 2016-10-03   20.0  2424.0
# 2016-10-04    NaN     NaN
# 2016-10-05    5.0   232.0
</code></pre>

python python-2.7 pandas datetimeindex