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['date']).hour):
group = list(grp)
logger.info(f'{timestamp} : {len(group)}')
-> i get the hour:count
array.
However i want to have a datetime:count
array as a result (where datetime
object represent one hour).
Does i have to build the datetime
object in lambda x
function? (i.e. get x['date']).hour
, x['date']).day
, x['date']).month
etc. and create a new datetime
using those values) or there is another way?
sample input(transactions) contains data for weeks/months:
[
{
'date': '2018-12-04T15:34:40+00:00',
'data': 'blabla'
},
{
'date': '2018-12-04T15:38:40+00:00',
'data': 'blabla'
},
{
'date': '2018-12-04T15:45:40+00:00',
'data': 'blabla'
},
]
sample output:
2018-12-04 13:00:00+00:00 : 6
2018-12-04 14:00:00+00:00 : 1
2018-12-04 15:00:00+00:00 : 2
Thank you
See Question&Answers more detail:os