Search code examples
pandasreindex

pandas reindex fill in missing dates


I have a dataframe with an index of dates. Each data is the first of the month. I want to fill in all missing dates in the index at a daily level.

I thought this should work:

daily=pd.date_range('2016-01-01', '2018-01-01', freq='D')
df=df.reindex(daily)

But it's returning NA in rows that should have data in (1st of the month dates) Can anyone see the issue?


Solution

  • Use reindex with parameter method='ffill' or resample with ffill for more general solution, because is not necessary create new index by date_range:

    df = pd.DataFrame({'a': range(13)},
                       index=pd.date_range('2016-01-01', '2017-01-01', freq='MS'))
    
    print (df)
                 a
    2016-01-01   0
    2016-02-01   1
    2016-03-01   2
    2016-04-01   3
    2016-05-01   4
    2016-06-01   5
    2016-07-01   6
    2016-08-01   7
    2016-09-01   8
    2016-10-01   9
    2016-11-01  10
    2016-12-01  11
    2017-01-01  12
    
    daily=pd.date_range('2016-01-01', '2018-01-01', freq='D')
    df1 = df.reindex(daily, method='ffill')
    

    Another solution:

    df1 = df.resample('D').ffill()  
    

    print (df1.head())
                a
    2016-01-01  0
    2016-01-02  0
    2016-01-03  0
    2016-01-04  0
    2016-01-05  0