user1802143
user1802143

Reputation: 15682

pandas dataframe interpolating missing days

I have a pandas dataframe with several columns. I have dates for business days excluding holidays and some other random holes. Is there an interpolation method for filling in these gaps and and getting a dataframe for all business days?

Upvotes: 4

Views: 1904

Answers (1)

szu
szu

Reputation: 972

You may use reindex() method of DataFrame:

x = pd.date_range('2013-01-01','2013-01-07',freq='D')
y = range(7)
df=pd.DataFrame(index=x,data=y,columns=['value'])

To add missing days (like holidays) you need to reindex it:

x2= pd.date_range('2013-01-01','2013-01-07',freq='4H')
df2=df.reindex(x2)

Then you may fill the gaps in values using interpolate() method of Series (different interpolation methods are available):

df2.value=df2.value.interpolate(method='linear')

Upvotes: 4

Related Questions