1- from gtfslite import GTFS
2- import pandas as pd
3- import datetime as dt
4- from .constants import ARBITRARY_SERVICE_ID , GTFS_DATE_STRFTIME
51import copy
2+ import datetime as dt
3+
64import numpy as np
5+ import pandas as pd
6+ from gtfslite import GTFS
7+
8+ from .constants import ARBITRARY_SERVICE_ID , GTFS_DATE_STRFTIME
79
810
911def subset_schedule_feed_to_one_date (feed : GTFS , service_date : dt .datetime ) -> GTFS :
1012 """Update a gtfslite feed object to only contain service on a specified service date"""
11- assert feed .valid_date (
12- service_date
13- ), f"Feed not valid on { service_date .isoformat ()} "
13+ assert feed .valid_date (service_date ), f"Feed not valid on { service_date .isoformat ()} "
1414 # Define a new calendar dates, since the synthetic feed will only be valid on the service date
1515 new_calendar_dates = pd .DataFrame (
1616 {
@@ -25,9 +25,7 @@ def subset_schedule_feed_to_one_date(feed: GTFS, service_date: dt.datetime) -> G
2525 trips_on_service_date ["service_id" ] = ARBITRARY_SERVICE_ID
2626 # Get only stop_times on the calendar date
2727 stop_times_on_service_date = feed .stop_times .loc [
28- feed .stop_times ["trip_id" ].isin (
29- trips_on_service_date ["trip_id" ]
30- ) # check if this is slow
28+ feed .stop_times ["trip_id" ].isin (trips_on_service_date ["trip_id" ]) # check if this is slow
3129 ].reset_index (drop = True )
3230 # TODO: evaluate whether it is necessary to remove stops, shapes, and transfers that do not have service
3331 # TODO: add any additional behavior for feeds with frequencies.txt
@@ -48,30 +46,22 @@ def time_string_to_time_since_midnight(time_str_series: pd.Series) -> pd.Series:
4846 Will give incorrect results on days where a DST transition occurs.
4947 """
5048 return time_str_series .str .split (":" ).map (
51- lambda s : (
52- int (s [0 ]) * 3600 + int (s [1 ]) * 60 + int (s [2 ]) if len (s ) == 3 else np .nan
53- )
49+ lambda s : (int (s [0 ]) * 3600 + int (s [1 ]) * 60 + int (s [2 ]) if len (s ) == 3 else np .nan )
5450 )
5551
5652
5753DEFAULT_SERVICE_DAY_START_SECONDS = 86400
5854
5955
60- def seconds_to_gtfs_format_time (
61- time_column : pd .Series , trip_id_column : pd .Series
62- ) -> pd .Series :
56+ def seconds_to_gtfs_format_time (time_column : pd .Series , trip_id_column : pd .Series ) -> pd .Series :
6357 """Convert time in seconds since midnight (from the warehouse) to gtfs format time"""
6458 # TODO: this will not handle dst correctly
6559 # Get all times as positive times since midnight
6660 absolute_time_relative_to_midnight = time_column .where (
6761 time_column > 0 , DEFAULT_SERVICE_DAY_START_SECONDS + time_column
6862 )
6963 # Get the first time of each trip
70- first_time = (
71- absolute_time_relative_to_midnight .groupby (trip_id_column )
72- .first ()
73- .rename ("first_time" )
74- )
64+ first_time = absolute_time_relative_to_midnight .groupby (trip_id_column ).first ().rename ("first_time" )
7565 # Merge trips with the last times
7666 trips_merged_with_first_times = pd .concat (
7767 [
@@ -87,13 +77,9 @@ def seconds_to_gtfs_format_time(
8777 validate = "many_to_one" ,
8878 )
8979 # Get the "GTFS Time" seconds by allowing
90- trips_merged_with_first_times ["gtfs_time_seconds" ] = trips_merged_with_first_times [
91- "midnight_time"
92- ].where (
93- trips_merged_with_first_times ["midnight_time" ]
94- >= trips_merged_with_first_times ["first_time" ],
95- trips_merged_with_first_times ["midnight_time" ]
96- + DEFAULT_SERVICE_DAY_START_SECONDS ,
80+ trips_merged_with_first_times ["gtfs_time_seconds" ] = trips_merged_with_first_times ["midnight_time" ].where (
81+ trips_merged_with_first_times ["midnight_time" ] >= trips_merged_with_first_times ["first_time" ],
82+ trips_merged_with_first_times ["midnight_time" ] + DEFAULT_SERVICE_DAY_START_SECONDS ,
9783 )
9884
9985 trips_merged_with_first_times ["hours" ] = (
0 commit comments