python - Group by 2 keys with only month in second key -
i trying group 2 keys in list of dict second key being datetime.date. need group nick_name , month , couple of sums, conditional sums etc.
in sql
select rw_worker_nick ,date_trunc( 'month', rw_date ) month ,sum(rw_end - rw_start) work_time results_woshi rw_script = 93 group rw_worker_nick ,date_trunc( 'month', rw_date ) order month
keys have same names db columns
there more conditions think easier in python on db level
this code far not know how group month
grouper = itemgetter("rw_worker_nick", "rw_date") result = [] itertools import groupby pprint import pprint key, grp in groupby(sorted(summary, key = grouper), grouper): temp_dict = dict(zip(["rw_worker_nick", "rw_date"], key)) result.append(temp_dict)
rather relying upon itemgetter
, write own key function ignores day in date:
grouper = lambda x: (x["rw_worker_nick"], x["rw_date"].replace(day=1))
this shifts every date first of month. if want group items same month in different years, can use x["rw_date"].month
instead of replace
call.
Comments
Post a Comment