log = np.array([['2015-05-08T15:46:06+0200', '2015-05-08T17:21:36+0200'], ['2015-05-08T17:10:53+0200', '2015-05-09T06:30:08+0200'], ['2015-08-09T22:38:45+0200', '2015-08-09T22:38:45+0200'], ['2015-08-09T22:41:33+0200', '2015-08-10T08:39:26+0200'], ['2015-08-11T17:25:52+0200', '2015-08-12T08:14:30+0200'], ['2015-08-13T13:12:08+0200', '2015-08-13T19:42:50+0200'], ['2015-08-13T17:30:18+0200', '2015-08-14T10:13:10+0200'], ['2015-10-20T13:42:07+0200', '2015-10-20T16:13:37+0200'], ['2015-10-21T10:27:05+0200', '2015-10-21T16:13:11+0200'], ['2015-12-05T13:28:51+0100', '2015-12-05T22:43:20+0200']], dtype='datetime64[s]') login = log[:,0] logoff = log[:,1] begin = GetMonday(np.min(login)) end = GetMonday(np.max(logoff), True) n_logs = log.shape[0]*1.0 week_td64 = np.timedelta64(1, 'W') nbWeek_td64 = int((end - begin) / week_td64) week = begin + np.arange(nbWeek_td64) * week_td64 tiledLogin = np.tile(login, nbWeek_td64) repeatedWeek = np.repeat(week, n_logs) repeatedWeek_order = np.repeat(np.arange(nbWeek_td64), n_logs) loginWeekMask = (tiledLogin >= repeatedWeek) & (tiledLogin < repeatedWeek+np.timedelta64(1,'W')) indexes = np.arange(len(loginWeekMask)) hours_spent = (logoff-login).astype('timedelta64[h]') weeks_entry = np.floor(indexes[loginWeekMask]/n_logs)repeatedWeek_order[loginWeekMask] print np.bincount(weeks_entry.astype('int64'), hours_spent.astype('float64')) #[ 14. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 36. # 0. 0. 0. 0. 0. 0. 0. 0. 0. 7. 0. 0. 0. 0. 0. # 8.] log = np.array([['2015-05-08T15:46:06+0200', '2015-05-08T17:21:36+0200'], ['2015-05-08T17:10:53+0200', '2015-05-09T06:30:08+0200'], ['2015-08-09T22:38:45+0200', '2015-08-09T22:38:45+0200'], ['2015-08-09T22:41:33+0200', '2015-08-10T08:39:26+0200'], ['2015-08-11T17:25:52+0200', '2015-08-12T08:14:30+0200'], ['2015-08-13T13:12:08+0200', '2015-08-13T19:42:50+0200'], ['2015-08-13T17:30:18+0200', '2015-08-14T10:13:10+0200'], ['2015-10-20T13:42:07+0200', '2015-10-20T16:13:37+0200'], ['2015-10-21T10:27:05+0200', '2015-10-21T16:13:11+0200'], ['2015-12-05T13:28:51+0100', '2015-12-05T22:43:20+0200']], dtype='datetime64[s]') login = log[:,0] logoff = log[:,1] begin = GetMonday(np.min(login)) end = GetMonday(np.max(logoff), True) n_logs = log.shape[0]*1.0 week_td64 = np.timedelta64(1, 'W') nbWeek_td64 = int((end - begin) / week_td64) week = begin + np.arange(nbWeek_td64) * week_td64 tiledLogin = np.tile(login, nbWeek_td64) repeatedWeek = np.repeat(week, n_logs) loginWeekMask = (tiledLogin >= repeatedWeek) & (tiledLogin < repeatedWeek+np.timedelta64(1,'W')) indexes = np.arange(len(loginWeekMask)) hours_spent = (logoff-login).astype('timedelta64[h]') weeks_entry = np.floor(indexes[loginWeekMask]/n_logs) print np.bincount(weeks_entry.astype('int64'), hours_spent.astype('float64')) #[ 14. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 36. # 0. 0. 0. 0. 0. 0. 0. 0. 0. 7. 0. 0. 0. 0. 0. # 8.] log = np.array([['2015-05-08T15:46:06+0200', '2015-05-08T17:21:36+0200'], ['2015-05-08T17:10:53+0200', '2015-05-09T06:30:08+0200'], ['2015-08-09T22:38:45+0200', '2015-08-09T22:38:45+0200'], ['2015-08-09T22:41:33+0200', '2015-08-10T08:39:26+0200'], ['2015-08-11T17:25:52+0200', '2015-08-12T08:14:30+0200'], ['2015-08-13T13:12:08+0200', '2015-08-13T19:42:50+0200'], ['2015-08-13T17:30:18+0200', '2015-08-14T10:13:10+0200'], ['2015-10-20T13:42:07+0200', '2015-10-20T16:13:37+0200'], ['2015-10-21T10:27:05+0200', '2015-10-21T16:13:11+0200'], ['2015-12-05T13:28:51+0100', '2015-12-05T22:43:20+0200']], dtype='datetime64[s]') login = log[:,0] logoff = log[:,1] begin = GetMonday(np.min(login)) end = GetMonday(np.max(logoff), True) n_logs = log.shape[0]*1.0 week_td64 = np.timedelta64(1, 'W') nbWeek_td64 = int((end - begin) / week_td64) week = begin + np.arange(nbWeek_td64) * week_td64 tiledLogin = np.tile(login, nbWeek_td64) repeatedWeek = np.repeat(week, n_logs) repeatedWeek_order = np.repeat(np.arange(nbWeek_td64), n_logs) loginWeekMask = (tiledLogin >= repeatedWeek) & (tiledLogin < repeatedWeek+np.timedelta64(1,'W')) hours_spent = (logoff-login).astype('timedelta64[h]') weeks_entry = repeatedWeek_order[loginWeekMask] print np.bincount(weeks_entry.astype('int64'), hours_spent.astype('float64')) #[ 14. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 36. # 0. 0. 0. 0. 0. 0. 0. 0. 0. 7. 0. 0. 0. 0. 0. # 8.] log = np.array([['2015-05-08T15:46:06+0200', '2015-05-08T17:21:36+0200'], ['2015-05-08T17:10:53+0200', '2015-05-09T06:30:08+0200'], ['2015-08-09T22:38:45+0200', '2015-08-09T22:38:45+0200'], ['2015-08-09T22:41:33+0200', '2015-08-10T08:39:26+0200'], ['2015-08-11T17:25:52+0200', '2015-08-12T08:14:30+0200'], ['2015-08-13T13:12:08+0200', '2015-08-13T19:42:50+0200'], ['2015-08-13T17:30:18+0200', '2015-08-14T10:13:10+0200'], ['2015-10-20T13:42:07+0200', '2015-10-20T16:13:37+0200'], ['2015-10-21T10:27:05+0200', '2015-10-21T16:13:11+0200'], ['2015-12-05T13:28:51+0100', '2015-12-05T22:43:20+0200']], dtype='datetime64[s]') login = log[:,0] logoff = log[:,1] begin = GetMonday(np.min(login)) end = GetMonday(np.max(logoff), True) n_logs = log.shape[0]*1.0 week_td64 = np.timedelta64(1, 'W') nbWeek_td64 = int((end - begin) / week_td64) week = begin + np.arange(nbWeek_td64) * week_td64 tiledLogin = np.tile(login, nbWeek_td64) tiledLogoff = np.tile(logoff, nbWeek_td64) repeatedWeek = np.repeat(week, n_logs) loginWeekMask = (tiledLogin >= repeatedWeek) & (tiledLogin < repeatedWeek+np.timedelta64(1,'W')) indexes = np.arange(len(loginWeekMask)) hours_spent = (tiledLogoff[loginWeekMask]logoff-tiledLogin[loginWeekMask]login).astype('timedelta64[h]') weeks_entry = np.floor(indexes[loginWeekMask]/n_logs) print np.bincount(weeks_entry.astype('int64'), hours_spent.astype('float64')) #[ 14. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 36. # 0. 0. 0. 0. 0. 0. 0. 0. 0. 7. 0. 0. 0. 0. 0. # 8.] log = np.array([['2015-05-08T15:46:06+0200', '2015-05-08T17:21:36+0200'], ['2015-05-08T17:10:53+0200', '2015-05-09T06:30:08+0200'], ['2015-08-09T22:38:45+0200', '2015-08-09T22:38:45+0200'], ['2015-08-09T22:41:33+0200', '2015-08-10T08:39:26+0200'], ['2015-08-11T17:25:52+0200', '2015-08-12T08:14:30+0200'], ['2015-08-13T13:12:08+0200', '2015-08-13T19:42:50+0200'], ['2015-08-13T17:30:18+0200', '2015-08-14T10:13:10+0200'], ['2015-10-20T13:42:07+0200', '2015-10-20T16:13:37+0200'], ['2015-10-21T10:27:05+0200', '2015-10-21T16:13:11+0200'], ['2015-12-05T13:28:51+0100', '2015-12-05T22:43:20+0200']], dtype='datetime64[s]') login = log[:,0] logoff = log[:,1] begin = GetMonday(np.min(login)) end = GetMonday(np.max(logoff), True) n_logs = log.shape[0]*1.0 week_td64 = np.timedelta64(1, 'W') nbWeek_td64 = int((end - begin) / week_td64) week = begin + np.arange(nbWeek_td64) * week_td64 tiledLogin = np.tile(login, nbWeek_td64) tiledLogoff = np.tile(logoff, nbWeek_td64) repeatedWeek = np.repeat(week, n_logs) loginWeekMask = (tiledLogin >= repeatedWeek) & (tiledLogin < repeatedWeek+np.timedelta64(1,'W')) indexes = np.arange(len(loginWeekMask)) hours_spent = (tiledLogoff[loginWeekMask]-tiledLogin[loginWeekMask]).astype('timedelta64[h]') weeks_entry = np.floor(indexes[loginWeekMask]/n_logs) print np.bincount(weeks_entry.astype('int64'), hours_spent.astype('float64')) #[ 14. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 36. # 0. 0. 0. 0. 0. 0. 0. 0. 0. 7. 0. 0. 0. 0. 0. # 8.] log = np.array([['2015-05-08T15:46:06+0200', '2015-05-08T17:21:36+0200'], ['2015-05-08T17:10:53+0200', '2015-05-09T06:30:08+0200'], ['2015-08-09T22:38:45+0200', '2015-08-09T22:38:45+0200'], ['2015-08-09T22:41:33+0200', '2015-08-10T08:39:26+0200'], ['2015-08-11T17:25:52+0200', '2015-08-12T08:14:30+0200'], ['2015-08-13T13:12:08+0200', '2015-08-13T19:42:50+0200'], ['2015-08-13T17:30:18+0200', '2015-08-14T10:13:10+0200'], ['2015-10-20T13:42:07+0200', '2015-10-20T16:13:37+0200'], ['2015-10-21T10:27:05+0200', '2015-10-21T16:13:11+0200'], ['2015-12-05T13:28:51+0100', '2015-12-05T22:43:20+0200']], dtype='datetime64[s]') login = log[:,0] logoff = log[:,1] begin = GetMonday(np.min(login)) end = GetMonday(np.max(logoff), True) n_logs = log.shape[0]*1.0 week_td64 = np.timedelta64(1, 'W') nbWeek_td64 = int((end - begin) / week_td64) week = begin + np.arange(nbWeek_td64) * week_td64 tiledLogin = np.tile(login, nbWeek_td64) repeatedWeek = np.repeat(week, n_logs) loginWeekMask = (tiledLogin >= repeatedWeek) & (tiledLogin < repeatedWeek+np.timedelta64(1,'W')) indexes = np.arange(len(loginWeekMask)) hours_spent = (logoff-login).astype('timedelta64[h]') weeks_entry = np.floor(indexes[loginWeekMask]/n_logs) print np.bincount(weeks_entry.astype('int64'), hours_spent.astype('float64')) #[ 14. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 36. # 0. 0. 0. 0. 0. 0. 0. 0. 0. 7. 0. 0. 0. 0. 0. # 8.] EDIT:
Tip: you can get rid of loop by np.tile() the log and np.repeat() the week.
FINAL ANSWER: Don't Don't read unless you give up.
def GetMonday(firstDay, forward=False): firstEntry = firstDay.astype('M8[D]') beforeMonday = np.busday_offset(firstEntry, forward*2-1, 'forward', [1,0,0,0,0,0,0]) if firstEntry-beforeMonday == np.timedelta64abs(7, 'D') or firstEntry-beforeMonday) == np.timedelta64(-7, 'D'): return firstEntry.astype('M8[s]') else: return beforeMonday.astype('M8[s]') EDIT:
Tip: you can get rid of loop by np.tile() the log and np.repeat() the week.
FINAL ANSWER: Don't read unless you give up.
def GetMonday(firstDay, forward=False): firstEntry = firstDay.astype('M8[D]') beforeMonday = np.busday_offset(firstEntry, forward*2-1, 'forward', [1,0,0,0,0,0,0]) if firstEntry-beforeMonday == np.timedelta64(7, 'D') or firstEntry-beforeMonday == np.timedelta64(-7, 'D'): return firstEntry.astype('M8[s]') else: return beforeMonday.astype('M8[s]') Tip: you can get rid of loop by np.tile() the log and np.repeat() the week.
FINAL ANSWER: Don't read unless you give up.
def GetMonday(firstDay, forward=False): firstEntry = firstDay.astype('M8[D]') beforeMonday = np.busday_offset(firstEntry, forward*2-1, 'forward', [1,0,0,0,0,0,0]) if abs(firstEntry-beforeMonday) == np.timedelta64(7, 'D'): return firstEntry.astype('M8[s]') else: return beforeMonday.astype('M8[s]') Loading
Loading
Loading
Loading
Loading
lang-py