Stop Times¶
StopTimes ¶
StopTimes(lf: Optional[LazyFrame] = None, frequencies: Optional[LazyFrame] = None, fixed_times: Optional[bool] = None, trips_lf: Optional[LazyFrame] = None)
Bases: FrequenciesMixin
Manages and processes GTFS stop_times.txt and frequencies.txt data.
This class provides a comprehensive pipeline for reading, cleaning, and enriching stop time data from GTFS feeds. It leverages Polars LazyFrames for efficient, memory-friendly processing.
Key Features
- Reads and combines data from multiple GTFS sources.
- Filters data by
stop_ids,trip_ids, and time windows. - Normalizes GTFS times to seconds within a 0-24 hour day, using a
next_dayflag for services crossing midnight. - Corrects invalid or missing
stop_sequencevalues. - Interpolates missing arrival/departure times (a more complex interpolation can be done together with the shapes file in the feed class).
- Calculates cumulative and total travel times for each trip.
- Expands frequency-based services into explicit stop times, creating new trip records for analysis.
- Generates canonical shape IDs by grouping trips with identical stop patterns and similar travel times.
Attributes:
| Name | Type | Description |
|---|---|---|
paths |
List[Path]
|
A list of Path objects to the GTFS directories. |
lf |
LazyFrame
|
The main LazyFrame containing the processed stop times data. |
fixed_times |
bool
|
A flag indicating if any stop times were interpolated. |
frequencies |
Optional[LazyFrame]
|
A LazyFrame for processed frequencies data, or None if not present. |
Wraps already-loaded stop_times/frequencies frames, or leaves them empty for load to fill in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lf
|
Optional[LazyFrame]
|
Optional pre-loaded, processed |
None
|
frequencies
|
Optional[LazyFrame]
|
Optional pre-loaded, processed |
None
|
fixed_times
|
Optional[bool]
|
Optional flag recording whether any times were interpolated during loading. |
None
|
trips_lf
|
Optional[LazyFrame]
|
Optional trips LazyFrame updated with any synthetic
trips generated from |
None
|
Source code in pyGTFSHandler/models/stop_times.py
load ¶
load(path: Union[str, Path, List[Union[str, Path]]], trips, start_time: Optional[datetime] = None, end_time: Optional[datetime] = None, stop_ids: Optional[List[str] | DataFrame | LazyFrame] = None, trip_ids: Optional[List[str]] = None, check_files: bool = False, min_file_id=0, collision_registry=None)
Initializes the StopTimes instance and runs the processing pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path, List[Union[str, Path]]]
|
A single path or a list of paths to GTFS directories. |
required |
start_time
|
Optional[datetime]
|
The start of a time window for initial filtering. If provided,
|
None
|
end_time
|
Optional[datetime]
|
The end of a time window for initial filtering. If provided,
|
None
|
stop_ids
|
Optional[List[str]]
|
A list of |
None
|
trip_ids
|
Optional[List[str]]
|
A list of |
None
|
Source code in pyGTFSHandler/models/stop_times.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
generate_shape_ids ¶
Groups trips by stop sequence and travel time to create canonical shape IDs.
This method generates new shape_ids. A shape is defined as a group of trips
that share the exact same sequence of stops and have a total travel duration
within a configurable tolerance (TRIP_ROUND_TIME, e.g., 2 minutes).
This is useful for identifying all trips that follow the same physical path
with a similar duration, effectively creating a shape_id when one is not
provided or is inconsistent in the GTFS data.
Returns:
| Type | Description |
|---|---|
LazyFrame
|
pl.LazyFrame: A LazyFrame where each row represents a unique "shape",
containing the generated |
Source code in pyGTFSHandler/models/stop_times.py
filter_by_time_range ¶
filter_by_time_range(start_time: datetime | time, end_time: datetime | time, strict: bool = True) -> Tuple[pl.LazyFrame, Optional[pl.LazyFrame]]
Filters stop_times and frequencies by a single time interval.
It handles intervals on the same date. If frequencies data is available,
it ensures that entire trips belonging to an overlapping frequency definition
are kept, even if not all their stops fall within the time bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_time
|
datetime | time
|
Start datetime of the filter interval. |
required |
end_time
|
datetime | time
|
End datetime of the filter interval. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[LazyFrame, Optional[LazyFrame]]
|
Tuple[pl.LazyFrame, Optional[pl.LazyFrame]]: A tuple containing:
- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in pyGTFSHandler/models/stop_times.py
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 | |
to_hhmmss ¶
Creates a Polars expression to convert seconds since midnight to a HH:MM:SS string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
str
|
The name of the column containing seconds (integer). |
required |
new_field
|
str
|
The desired name for the new HH:MM:SS string column. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
pl.Expr: A Polars expression that performs the conversion. Can be used in
a |