I am trying to insert missing weekdays in a time series dataframe such has
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')
data looks like this :
Out[300]:
price vol
date
2016-09-30 10 2020
2016-10-03 20 2424
2016-10-05 5 232
I can create a series of week days easily with pd.date_range()
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')
based on that DateTimeIndex I would like to add missing dates in my df
and fill column values with NaN so I get:
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
is there an easy way to do this? Thanks!