Python Itertools.groupby Datetime Series By Hour
The task is pretty simple and i'm able to partially accomplish it: from dateutil.parser import parse for timestamp, grp in itertools.groupby(transactions, lambda x: parse(x['d
Solution 1:
You need to use a datetime object as key, I used now as the date but you can use the original date:
import itertools
import datetime
from dateutil.parser import parse
transactions = ['2018-12-04 13:{}0:00+00:00'.format(i) for i inrange(6)] + \
['2018-12-04 14:{}0:00+00:00'.format(i) for i inrange(1)] + \
['2018-12-04 15:{}0:00+00:00'.format(i) for i inrange(2)]
for timestamp, grp in itertools.groupby(transactions, key=lambda x: datetime.datetime.combine(parse(x).date(), datetime.time(parse(x).hour, 0, 0, 0))):
count = list(grp)
print('{}:{}'.format(timestamp, len(count)))
Output
2018-12-04 13:00:00:62018-12-04 14:00:00:12018-12-04 15:00:00:2
Solution 2:
Since I'm a collections.Counter
fan:
from dateutils.parser import parse
from collections import Counter
counter = Counter()
for transaction in transactions:
counter.update(str(parse(transaction['date'][:-12])),)
for key,value insorted(counter.items()):
print(f'{key}: {value}')
And here is a solution using list comprehension:
from dateutils.parser import parse
from collections import Counter
counter = Counter([str(parse(x['date'][:-12])) for x in transactions])
for key,value insorted(counter.items()):
print(f'{key}: {value}')
Post a Comment for "Python Itertools.groupby Datetime Series By Hour"