以下是一个示例代码,以Python为例:
from datetime import datetime
def count_timestamps_by_minute(timestamps):
count_dict = {}
for timestamp in timestamps:
# 将时间戳转换为datetime对象
dt = datetime.fromtimestamp(timestamp)
# 将分钟设为0,秒和微秒设为0,以分组计算
dt = dt.replace(second=0, microsecond=0)
# 如果该分钟的时间戳已经存在于字典中,则数量加1,否则添加新的键值对
if dt in count_dict:
count_dict[dt] += 1
else:
count_dict[dt] = 1
return count_dict
# 示例数据
timestamps = [1609459200, 1609459205, 1609459210, 1609459215, 1609459215]
# 调用函数计算时间戳数量
result = count_timestamps_by_minute(timestamps)
# 输出结果
for dt, count in result.items():
print(dt, ":", count)
运行以上代码将输出以下结果:
2021-01-01 00:00:00 : 1
2021-01-01 00:01:00 : 3
这表示在第一个时间戳所在的分钟内,只有一个时间戳。而在第二个时间戳所在的分钟内,有三个时间戳。
上一篇:按分钟分组后,通过小时找到均值。
下一篇:按分钟过滤多个日期的数据框