Pandas asfreq 每周频率 [英] Pandas asfreq with weekly frequency
问题描述
我有一些设备的每周日志数据.对于某些设备,它从星期一开始,对于某些设备在星期三等.有时此数据中存在 ~month 的间隔,但我希望 DataFrame 索引仍包含每周具有 NaN 值的行.
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.
我正在尝试在 Python 中使用 asfreq('W')
,但我无法得到预期的结果.
I am trying to use asfreq('W')
in Python, but I cannot get what I expect.
示例:
我有什么:
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
我期望/想要的(注意 2 个带有 NaN 的新行):
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
我用 asfreq('W')
得到了什么:
Date Some_Value
==== ==========
2019-03-31 NaN
2019-04-07 NaN
2019-04-14 NaN
...................
所以,我得到所有 NaN
值和每个星期日的日期.但我不需要每个星期天的日期.我需要获取 DataFrame 的第一个日期(在 Pandas 的 groupby
中的组中的第一行,如果有很多时间序列)并每周重新采样第一行.
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.
是否可以直接使用 Pandas asfreq
实现?使用其他一些熊猫方法?还是应该是一些更复杂的自定义函数?
Is it achievable directly with pandas asfreq
? With some other pandas method? Or should it be some more complex custom function?
谢谢.
推荐答案
问题是你的数据是星期三的第一个值,星期四的最后两个值,所以 asfreq 返回 NaN
s,因为尝试改变它到周日工作日频率 - docs:
Problem is in your data are first values in Wednesday, last 2 in Thursday, so asfreq return NaN
s, because try change it to Sunday weekday freq - docs:
W-SUN 每周频率(星期日).与 'W'
W-SUN weekly frequency (Sundays). Same as 'W'
一种可能的解决方案,但 DatetimeIndex
已更改为星期日:
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
如果改变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
这篇关于Pandas asfreq 每周频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!