Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 751
user1802143 Avatar asked Nov 02 '25 12:11

user1802143


1 Answers

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')
like image 73
szu Avatar answered Nov 04 '25 06:11

szu