Add missing dates to pandas dataframe

前端 未结 5 2118
春和景丽
春和景丽 2020-11-22 09:47

My data can have multiple events on a given date or NO events on a date. I take these events, get a count by date and plot them. However, when I plot them, my two series do

5条回答
  •  隐瞒了意图╮
    2020-11-22 10:36

    A quicker workaround is to use .asfreq(). This doesn't require creation of a new index to call within .reindex().

    # "broken" (staggered) dates
    dates = pd.Index([pd.Timestamp('2012-05-01'), 
                      pd.Timestamp('2012-05-04'), 
                      pd.Timestamp('2012-05-06')])
    s = pd.Series([1, 2, 3], dates)
    
    print(s.asfreq('D'))
    2012-05-01    1.0
    2012-05-02    NaN
    2012-05-03    NaN
    2012-05-04    2.0
    2012-05-05    NaN
    2012-05-06    3.0
    Freq: D, dtype: float64
    

提交回复
热议问题