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 DataFrame index to still contain rows for each week with NaN value.
I am trying to use asfreq('W')
in Python, but I cannot get what I expect.
Example:
What I have:
Date Some_Value
==== ==========
2019-04-10 2
2019-04-17 1
2019-04-24 3
2019-05-01 1
2019-05-08 3
2019-05-15 2
2019-06-06 3
2019-06-13 2
What I expect/want to have (note 2 new rows with NaNs):
Date Some_Value
==== ==========
2019-04-10 2
2019-04-17 1
2019-04-24 3
2019-05-01 1
2019-05-08 3
2019-05-15 2
2019-05-22 NaN
2019-05-30 NaN
2019-06-06 3
2019-06-13 2
What I get with asfreq('W')
:
Date Some_Value
==== ==========
2019-03-31 NaN
2019-04-07 NaN
2019-04-14 NaN
...................
So, I get all NaN
values and the dates from each Sunday. But I do not need dates from each Sunday. I need to take the first date of a DataFrame (of first row in a group in pandas' groupby
in case of many time-series) and resample weekly form that first row.
Is it achievable directly with pandas asfreq
? With some other pandas method? Or should it be some more complex custom function?
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…