我有一个带有不规则时间戳的时间序列数据集。如何将其转换为以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
答案 0 :(得分:0)
因为您没有显示任何代码,或者您从哪里获得数据,所以我只能提供快速而肮脏的建议。因此,没有使用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