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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -