def extract_delays(feed):
    """
    Given a GTFSR feed (FeedMessage instance), extract the delays from
    the feed and return a DataFrame with the columns:

    - route_id
    - trip_id
    - stop_id
    - stop_sequence
    - arrival_delay
    - departure_delay

    If the feed has no trip updates, then return an empty DataFrame.
    """
    rows = []
    for e in feed.entity:
        if not e.HasField('trip_update'):
            continue
        tu = e.trip_update
        rid = tu.trip.route_id
        tid = tu.trip.trip_id
        stu = tu.stop_time_update
        for x in stu:
            stop_sequence = int(x.stop_sequence)
            stop_id = str(x.stop_id)
            delay = {}
            for key in ['arrival', 'departure']:
                if x.HasField(key):
                    delay[key] = getattr(x, key).delay
                else:
                    delay[key] = np.nan
            rows.append((rid, tid, stop_sequence, stop_id,
              delay['arrival'], delay['departure']))

    f = pd.DataFrame(rows, columns=[
      'route_id', 'trip_id', 'stop_sequence', 'stop_id',
      'arrival_delay', 'departure_delay'])
    f = f.sort_values(['route_id', 'trip_id', 'stop_sequence'])
    f.index = range(f.shape[0])
    return f
def extract_delays ( feed ) : rows = [ ] for e in feed . entity : if not e . HasField ( 'trip_update' ) : continue tu = e . trip_update rid = tu . trip . route_id tid = tu . trip . trip_id stu = tu . stop_time_update for x in stu : stop_sequence = int ( x . stop_sequence ) stop_id = str ( x . stop_id ) delay = { } for key in [ 'arrival' , 'departure' ] : if x . HasField ( key ) : delay [ key ] = getattr ( x , key ) . delay else : delay [ key ] = np . nan rows . append ( ( rid , tid , stop_sequence , stop_id , delay [ 'arrival' ] , delay [ 'departure' ] ) ) f = pd . DataFrame ( rows , columns = [ 'route_id' , 'trip_id' , 'stop_sequence' , 'stop_id' , 'arrival_delay' , 'departure_delay' ] ) f = f . sort_values ( [ 'route_id' , 'trip_id' , 'stop_sequence' ] ) f . index = range ( f . shape [ 0 ] ) return f
Given a GTFSR feed (FeedMessage instance), extract the delays from
    the feed and return a DataFrame with the columns:

    - route_id
    - trip_id
    - stop_id
    - stop_sequence
    - arrival_delay
    - departure_delay

    If the feed has no trip updates, then return an empty DataFrame.