I am learning numpy through exercices. I've got some trouble with this one. I've got to code a function which take a np_array as argument and return a new np_array. the argument look like :
>> log 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]') Log contains info about connexion to a server. First element of each row is a login date and the second is the corresponding logout date.
The new np_array should return the number of hours where the server was connected, per weeks between, the monday preceding the first connection and the monday after the connection.
>> func(log) array([[time_connected_week1, time_connected_week2, time_connected_week3, ... time_connected_weekn]], dtype='timedelta64[s]' week1 (weekn) must fit the first (last) week of the log array.
I have written the following code:
def func(log): begin = np.datetime64("2015-05-04") # first monday end = np.datetime64("2015-12-07") # last monday week_td64 = np.timedelta64(1, 'W') nbWeek_td64 = int((end - begin) / week_td64) week = begin + np.arange(nbWeek_td64) * week_td64 # arange(week1, weekn) weekHours = [] # list to store return values for w in week: mask1 = log[:,0] > w mask2 = log[:,0] < w + week_td64 l = log[mask1 & mask2] # get log row matching the current week totalweek = (l[:,1] - l[:,0]).sum() #compute sum of result weekHours.append(totalweek) #save value return np.array(weekHours) I've got two questions concerning my code:
1/ how can I find the first monday automaticaly ? np.datetime64 does not support weekday(). Do I have to use datetime.datetime ?
2/ How can I get rid of the loop ? I've been said that numpy was a lot about getting rid of loop. I am sure we can do this with fancy slicing.