Pandas asfreq with weekly frequency

前端 未结 1 1481
北荒
北荒 2020-12-20 02:42

I have weekly data of logs for some devices. For some device it start on Monday, for some on Wednesday etc. Sometimes there are gaps of ~month in this data, but I want the D

相关标签:
1条回答
  • 2020-12-20 03:10

    Problem is in your data are first values in Wednesday, last 2 in Thursday, so asfreq return NaNs, because try change it to Sunday weekday freq - docs:

    W-SUN weekly frequency (Sundays). Same as 'W'

    One possible solution, but DatetimeIndex is changed for Sundays:

    print (df.resample('W').first())
                Some_Value
    Date                  
    2019-04-14         2.0
    2019-04-21         1.0
    2019-04-28         3.0
    2019-05-05         1.0
    2019-05-12         3.0
    2019-05-19         2.0
    2019-05-26         NaN
    2019-06-02         NaN
    2019-06-09         3.0
    2019-06-16         2.0
    

    If change frequency in asfreq:

    print (df.asfreq('W-Wed'))
                Some_Value
    Date                  
    2019-04-10         2.0
    2019-04-17         1.0
    2019-04-24         3.0
    2019-05-01         1.0
    2019-05-08         3.0
    2019-05-15         2.0
    2019-05-22         NaN
    2019-05-29         NaN
    2019-06-05         NaN
    2019-06-12         NaN
    
    print (df.asfreq('W-Thu'))
                Some_Value
    Date                  
    2019-04-11         NaN
    2019-04-18         NaN
    2019-04-25         NaN
    2019-05-02         NaN
    2019-05-09         NaN
    2019-05-16         NaN
    2019-05-23         NaN
    2019-05-30         NaN
    2019-06-06         3.0
    2019-06-13         2.0
    
    0 讨论(0)
提交回复
热议问题