Nik*_*il 8 python time-series dataframe pandas
我在以下形式的pandas中有一个数据帧:
timestamps light
7 2004-02-28 00:58:45 150.88
26 2004-02-28 00:59:45 143.52
34 2004-02-28 01:00:45 150.88
42 2004-02-28 01:01:15 150.88
59 2004-02-28 01:02:15 150.88
Run Code Online (Sandbox Code Playgroud)
请注意,索引不是时间戳列.但我想重新取样(或箱莫名其妙的数据)来反映每分钟,小时,天等的光柱的平均值..我进去看了resample大熊猫报价方法,它要求数据框有一个DATATIME指数工作的方法(除非我误解了这一点).
所以我的第一个问题是,我可以重新指数有时间戳作为索引数据帧(注意,并非每一行都有一个唯一的时间戳和每个时间戳,大约有30行相同的时间戳,每个代表传感器).
如果没有,是否还有其他方法可以实现另一个具有每小时,每天,每月等的平均值的数据帧.?
任何帮助,将不胜感激.
你是对的-需要DatetimeIndex,TimedeltaIndex或PeriodIndex其他错误:
TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但得到'Index'的实例
所以你必须首先reset_index,set_index如果原始index是重要的:
print (df.reset_index().set_index('timestamps'))
index light
timestamps
2004-02-28 00:58:45 7 150.88
2004-02-28 00:59:45 26 143.52
2004-02-28 01:00:45 34 150.88
2004-02-28 01:01:15 42 150.88
2004-02-28 01:02:15 59 150.88
Run Code Online (Sandbox Code Playgroud)
如果不是set_index:
print (df.set_index('timestamps'))
light
timestamps
2004-02-28 00:58:45 150.88
2004-02-28 00:59:45 143.52
2004-02-28 01:00:45 150.88
2004-02-28 01:01:15 150.88
2004-02-28 01:02:15 150.88
Run Code Online (Sandbox Code Playgroud)
然后resample:
print (df.reset_index().set_index('timestamps').resample('1D').mean())
index light
timestamps
2004-02-28 33.6 149.408
Run Code Online (Sandbox Code Playgroud)
对于 0.19.0 及更新版本的 Pandas,您可以使用on关键字:
df.resample('H', on='timestamps').mean()
Run Code Online (Sandbox Code Playgroud)
结果:
light
timestamps
2004-02-28 00:00:00 147.20
2004-02-28 01:00:00 150.88
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12119 次 |
| 最近记录: |