Skip to content
Advertisement

Easiest method to interpolate over missing dates in a time series?

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.
Sample of excel data

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
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement