我有两个数据流,都是两个(时间戳,值)元组。
即:
[(2013-04-03T22:16:36+0000, 2334.5), (2013-04-03T22:46:36+0000, 43543.23), ...]
这个想法是其中一个将被“优先”而不是“不被优先”,我想创建一个单一的时间序列,该时间序列是由于较高优先级流(如果可用)的结果,而后退到最不优选流。
我的想法是将两个流中的值的时间戳放入存储桶中,并使用存储桶作为DataFrame的索引,每个流具有一列,每个存储桶中都有(时间戳,值)元组的列表。然后,我可以只浏览每个存储桶,例如,选择得分最高的存储桶。
数据框如下所示:
timestamp stream1 stream2
2013-04-03 00:00:00 [(2013-04-03T00:16:36+0000, 2334.5), [(2013-04-03T00:17:36+0000, 2314.5)]
(2013-04-03T00:17:36+0000, 2314.5)]
2013-04-03 00:30:00 [(2013-04-03T00:43:44+0000, 43543.23), [(2013-04-03T00:47:36+0000, 2364.5)]
(2013-04-03T00:54:24+0000, 4443.23)]
2013-04-03 01:00:00 [] [(2013-04-03T01:01:30+0000, 34.34)]
2013-04-03 01:30:00 [(2013-04-03T01:35:32+0000, 238734.3)] [(2013-04-03T01:45:32+0000, 238734.3)]
在这种情况下,时间戳记已放入半小时存储桶中,并且stream1是首选的流。对于00:00的存储桶,将选择流1中的两个点,对于00:30的存储桶,将选择流1中的两个点,对于01:00的存储桶,将选择stream2中的单个点作为stream1没有数据,对于01:30的存储桶,将选择stream1中的单个数据点,因为它是首选流。
我将如何去做呢?我尝试创建数据框并使用
resample('h', how='count')
拆分为小时计数,然后使用groupby
,但无法完全将时间戳记入存储桶并为每个存储桶的每个流创建值列表。 最佳答案
我有一个解决方案,但是我不确定它的效率(我自己是Pandas noob),或者是否有一种更“熊猫风格”的方法:
hh = date_range('2013-01-01T00:30:00', periods=48, freq='1800S')
s5 = date_range('2013-01-01', freq='5S', end='2013-01-02')
s5 = s5[:720] + s5[1440:] # introduce a gap in the 5 second data
hh_d = Series(hh.astype('int') / 10 ** 9, index=hh)
s5_d = Series(s5.astype('int') / 10 ** 9, index=s5)
df = DataFrame({
'hh': hh_d,
'5s': s5_d,
})
# Make a grouping, for simplicity by day, hour
grp = df.groupby(lambda x: (x.day, x.hour))
result = TimeSeries()
for name, group in grp:
winner = None
for column in group.keys(): # iterate over the columns (streams)
data = group[column].dropna() # ditch any NaNs that will be present
# next, perform the test (in this case, just the length)
if not winner or len(data) > len(group[winner].dropna()):
winner = column
# finally, add the data to the result set.
result = result.append(group[winner].dropna())
在5秒间隔内检查结果可得出:
ipdb> result[719:725]
2013-01-01 00:59:55 1357001995
2013-01-01 01:00:00 1357002000
2013-01-01 01:30:00 1357003800
2013-01-01 02:00:00 1357005600
2013-01-01 02:00:05 1357005605
2013-01-01 02:00:10 1357005610
dtype: float64
这表明在间隙期间选择了半小时流。
上面的示例基于组中每一列的长度,但是我想可以应用任何测试。
希望有更多熊猫经验的人可以详细说明我的稻草人答案!