将不规则时间戳数据转换为等间隔时间序列
问题描述:
我有时间序列数据集,其中包含不规则时间戳。我怎样才能将其转换为以5个分钟间隔将不规则时间戳数据转换为等间隔时间序列
Datetime Value
11/6/2017 07:56:41 16
11/6/2017 08:01:22 16
11/6/2017 08:06:39 23
11/6/2017 08:11:56 23
11/6/2017 08:23:18 25
11/6/2017 08:29:11 31
11/6/2017 08:36:40 33
11/6/2017 08:42:05 39
11/6/2017 08:47:42 39
11/6/2017 08:53:08 37
11/6/2017 08:58:28 39
11/6/2017 09:03:50 39
11/6/2017 09:09:19 39
答
设置因为你没有表现出任何代码,或你在哪里得到你的数据,我只能提供一个快速和肮脏建议一个正规化的时间序列。所以没有datetime
或math
的用法。
data = """
11/6/2017 07:56:41 16
11/6/2017 08:01:22 16
11/6/2017 08:06:39 23
11/6/2017 08:11:56 23
11/6/2017 08:23:18 25
11/6/2017 08:29:11 31
11/6/2017 08:36:40 33
11/6/2017 08:42:05 39
11/6/2017 08:47:42 39
11/6/2017 08:53:08 37
11/6/2017 08:58:28 39
11/6/2017 09:03:50 39
11/6/2017 09:09:19 39""".strip()
def rewrite_line(line):
date, time, count = line.split()
hours, minutes, seconds = time.split(':')
minutes, seconds = (int(minutes)/5)*5, 0
time = '%02s:%02d:%02d' % (hours, minutes, seconds)
return '%s %s %s' % (date, time, count)
for line in data.splitlines():
print rewrite_line(line)
应打印类似:
11/6/2017 07:55:00 16
11/6/2017 08:00:00 16
11/6/2017 08:05:00 23
11/6/2017 08:10:00 23
11/6/2017 08:20:00 25
11/6/2017 08:25:00 31
11/6/2017 08:35:00 33
11/6/2017 08:40:00 39
11/6/2017 08:45:00 39
11/6/2017 08:50:00 37
11/6/2017 08:55:00 39
11/6/2017 09:00:00 39
11/6/2017 09:05:00 39