I have some stock market data in excel covering the past 20 years or so which contains gaps from holidays and weekends. I wish to interpolate over those missing dates to obtain the approximate stock index for those days.
I’ve read both columns into Python using pandas and assigned them to their respective variables. What would be the best method to go about detecting the gaps in the dates and interpolating across them?
Advertisement
Answer
Pandas has methods specifically for this type of situation:
df.interpolate() # will fill in based on the linear average of the before and after df.fillna(method='ffill') # forward fill df.fillna(method='bfill') # backward fill