Looping over weekday in python time object
Question: I have a dataset of drivers' travel diaries. For each trip there is an
associated start time, end time and day of week in a csv file. There are no
dates associated with the trips.

I have now got the data into python where each start time and end time has the
weekday attached to it like so:

    
    
    time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=23, 
                     tm_min=45, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
    
    print journey['BeginTime'][2].tm_wday, journey['BeginTime'][2].tm_hour
    

Which returns 0 for a Monday and 23 for the hour.

There's 11,000 of these trips and what I want to get is a weekly profile of
the number of cars which are driving based on time of day.

This can be inferred by counting the number of trips that are between their
respective ['BeginTime'] and ['EndTime'] interval over a specified time
interval. A five minute interval is sufficient as the data is to the nearest
five minutes.

Is there an elegant python way to do this? Something like:

    
    
    for fiveMinutes in Week:
    count = 0
        for trip in range(len(journey['BeginTime']):
            if journey['BeginTime'][trip] == fiveMinutes
                   or (journey['BeginTime'][trip] < fiveMinutes 
                       and journey['EndTime'][trip] > fiveMinutes):
               count = count + 1
    carCount[fiveMinutes] = count
    


Answer: In case this helps, here is an idea ...

    
    
    from datetime import datetime, timedelta
    
    # This does not check for crossing from Sunday to Monday
    def convert_dt(start_dt, journey):
        begin_weekday, begin_hour, begin_minute = journey[0]
        end_weekday, end_hour, end_minute = journey[1]
    
        begin_dt = start_dt + timedelta(days=begin_weekday)
        begin_dt += timedelta(hours=begin_hour, minutes=begin_minute)
    
        end_dt = start_dt + timedelta(days=end_weekday)
        end_dt += timedelta(hours=end_hour,minutes=end_minute)
        return (begin_dt, end_dt)
    
    def get_slot_journeys(start_dt, journeys):           
        next_dt = start_dt
        slot_count =  60/5 * 24 * 7
        slot_dict = {}
    
        journey_dts = []
        #convert journey begin and end to datetimes
        for index in range(len(journeys['begin_weekday'])):
            next_journey = [(journeys['begin_weekday'][index],
                             journeys['begin_hour'][index],
                             journeys['begin_minute'][index],),
                            (journeys['end_weekday'][index],
                             journeys['end_hour'][index],
                             journeys['end_minute'][index],)
                           ]
            journey_dts.append(convert_dt(start_dt, next_journey))
    
        for slot in range(slot_count):
            slot_dict[next_dt] = 0
            for journey_start, journey_end in journey_dts:
                if next_dt >= journey_start and next_dt <= journey_end:
                    slot_dict[next_dt] = slot_dict[next_dt] + 1                    
    
            next_dt += timedelta(minutes=(5))
    
        return slot_dict
    
    if __name__ == "__main__":
        start_dt = datetime(2012, 1, 2, 0, 0)    
    
        journeys = {'begin_weekday': [0, 0],
                    'begin_hour': [14, 18],
                    'begin_minute': [20, 30],
                    'end_weekday': [0, 1],
                    'end_hour': [19, 12],
                    'end_minute': [15, 55],
                   }
        slot_dict = get_slot_journeys(start_dt, journeys)       
        slot_keys = slot_dict.keys()
        slot_keys.sort()
    
        for key in slot_keys:
            if slot_dict[key]:    
                print key, slot_dict[key]
    

