I am iterating over a dictionary that contains data from a SQL database and I want to count the number of times that user
values appear between initial_date
and ending_date
, however, I am having some problems when I try to parse the Timestamp values. This is the code I have
initial_date = datetime(2017,09,01,00,00,00)
ending_date = datetime(2017,09,30,00,00,00)
dictionary sample that I got
sample = {'id': 100008222, 'sector name': 'BONGOX', 'site name': 'BONGO', 'region': 'EMEA',
'open date': Timestamp('2017-09-11 00:00:00'), 'mtti': '16', 'mttr': '1', 'mttc': '2','user':'John D.'},
{'id': 100008234, 'sector name': 'BONGOY', 'site name': 'BONGO', 'region': 'EMEA',
'open date': Timestamp('2017-09-09 12:05:00'), 'mtti': '1', 'mttr': '14', 'mttc': '7','user':'John D.'}
{'id': 101108234, 'sector name': 'BONGOA', 'site name': 'BONGO', 'region': 'EMEA',
'open date': Timestamp('2017-09-01 10:00:00'), 'mtti': '1', 'mttr': '12', 'mttc': '1','user':'John C.'}
{'id': 101108254, 'sector name': 'BONGOB', 'site name': 'BONGO', 'region': 'EMEA',
'open date': Timestamp('2017-09-02 20:00:00'), 'mtti': '2', 'mttr': '19', 'mttc': '73','user':'John C.'}
This is the code that I use to count the number of times user
values appear between initial_date
and ending_date
from datetime import time, datetime
from collections import Counter
#This approach does not work
Counter([li['user'] for li in sample if initial_date < dateutil.parser.parse(time.strptime(str(li.get(
'open date'),"%Y-%m-%d %H:%M:%S") < ending_date])
The code from above does not work because I encountered the error decoding to str: need a bytes-like object, Timestamp found
I have two questions:
- How can I parse this Timestamp value that I encountered in these dictionaries?
- I read in this post Why Counter is slow that Collections.Counter is a slow method compared to other approaches to count the number of times an item appears. If want to avoid using Counter.Collections, how can I achieve my desired result of counting the number of times
user
values appear between these dates?