gpt4 book ai didi

python - 按 20 秒间隔对时间戳进行分组

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:02 25 4
gpt4 key购买 nike

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 秒内进行分组

最佳答案

组需要有秒字段:

代码:

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

关于python - 按 20 秒间隔对时间戳进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43288758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com