Add missing dates to pandas dataframe

前端 未结 5 2186
春和景丽
春和景丽 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:12

    You could use Series.reindex:

    import pandas as pd
    
    idx = pd.date_range('09-01-2013', '09-30-2013')
    
    s = pd.Series({'09-02-2013': 2,
                   '09-03-2013': 10,
                   '09-06-2013': 5,
                   '09-07-2013': 1})
    s.index = pd.DatetimeIndex(s.index)
    
    s = s.reindex(idx, fill_value=0)
    print(s)
    

    yields

    2013-09-01     0
    2013-09-02     2
    2013-09-03    10
    2013-09-04     0
    2013-09-05     0
    2013-09-06     5
    2013-09-07     1
    2013-09-08     0
    ...
    

提交回复
热议问题