I have a list of times (as datetime objects) with a record of counts (photons as it were). I would like to bin the counts into one-minute bins. I thought I could do this with a histogram, but numpy histogram does not play with datetime objects. How do I bin this data into one-minute intervals?
Here is a sample of my data:
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]
I've thought about using pandas dataframes, but I'm struggling to implement it and I'm not sure if this is the correct path.
You can discard the seconds
part from the time, and add up the counts
in a minute using a dictionary:
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)
Output:
['2019-02-04T06:11', '2019-02-04T06:12']
[23212, 30354]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.