我有一个时间列表(作为日期时间对象),并带有一个计数记录(原为光子)。我想将计数分到一分钟的分箱中。我以为可以用直方图来做到这一点,但是numpy直方图不能与日期时间对象一起玩。如何将这些数据分成1分钟的间隔?
以下是我的数据示例:
Times = ['2019-02-04T06:11:31', '2019-02-04T06:11:33',
'2019-02-04T06:11:35', '2019-02-04T06:11:37',
'2019-02-04T06:11:39', '2019-02-04T06:11:41',
'2019-02-04T06:11:43', '2019-02-04T06:11:45',
'2019-02-04T06:11:47', '2019-02-04T06:11:49',
'2019-02-04T06:11:51', '2019-02-04T06:11:53',
'2019-02-04T06:11:55', '2019-02-04T06:11:57',
'2019-02-04T06:11:59', '2019-02-04T06:12:01',
'2019-02-04T06:12:03', '2019-02-04T06:12:05',
'2019-02-04T06:12:07', '2019-02-04T06:12:09',
'2019-02-04T06:12:11', '2019-02-04T06:12:13',
'2019-02-04T06:12:15', '2019-02-04T06:12:17',
'2019-02-04T06:12:19', '2019-02-04T06:12:21',
'2019-02-04T06:12:23', '2019-02-04T06:12:25',
'2019-02-04T06:12:27', '2019-02-04T06:12:29',
'2019-02-04T06:12:31', '2019-02-04T06:12:33',
'2019-02-04T06:12:35', '2019-02-04T06:12:37',
'2019-02-04T06:12:39', '2019-02-04T06:12:41']
Counts = [1628, 1613, 1622, 1650, 1527, 1622, 1585, 1529, 1580,
1497, 1523, 1450, 1453, 1479, 1454, 1423, 1495, 1429,
1429, 1455, 1512, 1544, 1441, 1463, 1463, 1453, 1427,
1378, 1409, 1409, 1457, 1461, 1476, 1419, 1386, 1425]
我已经考虑过使用pandas数据帧,但是我正在努力实现它,并且不确定这是否是正确的路径。
答案 0 :(得分:0)
您可以暂时丢弃seconds
部分,并使用字典在一分钟内将counts
加起来:
dict_counts = {} # key = timestamp, value = cumulative count
for t,c in zip(Times, Counts):
t = t.split(':')
new_t = t[0] + ':' + t[1]
dict_counts[new_t] = dict_counts.get(new_t, 0) + c
NewTimes = list(dict_counts.keys())
NewCounts = list(dict_counts.values())
print(NewTimes)
print(NewCounts)
输出:
['2019-02-04T06:11', '2019-02-04T06:12']
[23212, 30354]