from datetime import datetime, timedelta
d = datetime.now()
data = [d + timedelta(minutes=i) for i in range(100)]
# prepare and group the data
from itertools import groupby
def get_key(d):
# group by 30 minutes
k = d + timedelta(minutes=-(d.minute % 20))
return datetime(k.year, k.month, k.day, k.hour, k.minute, 0)
g = groupby(sorted(data), key=get_key)
# print data
for key, items in g:
print(key)
for item in items:
print('-', item)
我有一个大型时间戳数据集,我想将它分组到20秒窗口。上面的代码非常适合在几分钟内对其进行分组,但我无法在20秒内将其分组工作
答案 0 :(得分:3)
该组需要有一个秒字段:
<强>代码:强>
import datetime as dt
def get_key(d):
# group by 20 seconds
k = d - dt.timedelta(seconds=d.second % 20)
return dt.datetime(k.year, k.month, k.day, k.hour, k.minute, k.second)
测试代码:
d = dt.datetime.now()
data = [d + dt.timedelta(seconds=i*5) for i in range(20)]
# prepare and group the data
import itertools as it
g = it.groupby(sorted(data), key=get_key)
# print data
for key, items in g:
print(key)
for item in items:
print('-', item)
<强>结果:强>
2017-04-07 17:51:00
- 2017-04-07 17:51:16.372000
2017-04-07 17:51:20
- 2017-04-07 17:51:21.372000
- 2017-04-07 17:51:26.372000
- 2017-04-07 17:51:31.372000
- 2017-04-07 17:51:36.372000
2017-04-07 17:51:40
- 2017-04-07 17:51:41.372000
- 2017-04-07 17:51:46.372000
- 2017-04-07 17:51:51.372000
- 2017-04-07 17:51:56.372000
2017-04-07 17:52:00
- 2017-04-07 17:52:01.372000
- 2017-04-07 17:52:06.372000
- 2017-04-07 17:52:11.372000
- 2017-04-07 17:52:16.372000
2017-04-07 17:52:20
- 2017-04-07 17:52:21.372000
- 2017-04-07 17:52:26.372000
- 2017-04-07 17:52:31.372000
- 2017-04-07 17:52:36.372000
2017-04-07 17:52:40
- 2017-04-07 17:52:41.372000
- 2017-04-07 17:52:46.372000
- 2017-04-07 17:52:51.372000