Feed¶
Feed ¶
Feed(gtfs_dirs: Union[List[Union[str, Path]], str, Path], aoi: Optional[Union[GeoDataFrame, GeoSeries]] = None, stop_group_distance: float = 0, start_date: Optional[datetime | date] = None, end_date: Optional[datetime | date] = None, date_type: Optional[list[str] | str] = None, start_time: Optional[datetime | time] = None, end_time: Optional[datetime | time] = None, route_types: Optional[list[int] | list[str] | int | str] = None, service_ids: Optional[List[str]] = None, trip_ids: Optional[List[str]] = None, stop_ids: Optional[List[str]] = None, route_ids: Optional[List[str]] = None, check_files: bool = True, min_file_id: int = 0, load_shapes: bool = True)
Bases: FeedFilteringMixin, FeedAnalysisMixin, FeedEdgeAnalysisMixin
Represents and orchestrates a complete GTFS feed.
This class integrates various GTFS components (calendar, trips, stops, etc.) into a single, queryable data structure. It handles the loading, filtering, and joining of all relevant GTFS files.
Attributes:
| Name | Type | Description |
|---|---|---|
gtfs_dir |
List[Path]
|
List of Path objects for the directories containing GTFS data. |
calendar |
Calendar
|
An object handling |
routes |
Routes
|
An object handling |
trips |
Trips
|
An object handling |
stops |
Stops
|
An object handling |
stop_times |
StopTimes
|
An object handling |
trip_shape_ids_lf |
LazyFrame
|
A LazyFrame mapping generated shape IDs to trip IDs. |
shapes |
Shapes
|
An object handling |
lf |
LazyFrame
|
The main, integrated LazyFrame containing denormalized schedule data. |
Loads, filters and integrates one or more GTFS directories into a Feed.
This is the main entry point of the library: pass one or more
uncompressed GTFS directories (or zip files) and get back a Feed
whose self.lf is a single denormalized LazyFrame joining stops,
stop_times, trips, routes, calendar and shapes. Internally this
just calls self.load(...) (component loading/filtering), then
self.load_shapes(...) and self.build_lf(...) -- see those
methods' docstrings for the detailed pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gtfs_dirs
|
Union[List[Union[str, Path]], str, Path]
|
One or more paths to GTFS directories or |
required |
aoi
|
Optional[Union[GeoDataFrame, GeoSeries]]
|
Optional Area of Interest (GeoDataFrame/GeoSeries) used to geospatially filter stops. |
None
|
stop_group_distance
|
float
|
If greater than 0, cluster stops within
this distance of each other under a shared |
0
|
start_date
|
Optional[datetime | date]
|
Inclusive lower bound on service dates to load. |
None
|
end_date
|
Optional[datetime | date]
|
Inclusive upper bound on service dates to load. |
None
|
date_type
|
Optional[list[str] | str]
|
Optional weekday/weekend/holiday classification
filter (see |
None
|
start_time
|
Optional[datetime | time]
|
Lower bound of the daily time window to load. |
None
|
end_time
|
Optional[datetime | time]
|
Upper bound of the daily time window to load. |
None
|
route_types
|
Optional[list[int] | list[str] | int | str]
|
Optional route type filter (names, codes, or a
mix); |
None
|
service_ids
|
Optional[List[str]]
|
Optional explicit |
None
|
trip_ids
|
Optional[List[str]]
|
Optional explicit |
None
|
stop_ids
|
Optional[List[str]]
|
Optional explicit |
None
|
route_ids
|
Optional[List[str]]
|
Optional explicit |
None
|
check_files
|
bool
|
If |
True
|
min_file_id
|
int
|
Starting index used to tag rows with which source
|
0
|
load_shapes
|
bool
|
If |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any of |
Exception
|
If the combination of filters leaves no stops, routes, services, trips, or stop_times. |
Source code in pyGTFSHandler/feed.py
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 | |
get_representative_date ¶
get_representative_date(start_date: Optional[datetime | date] = None, end_date: Optional[datetime | date] = None, date_type: Optional[str] = 'weekday', route_types: Optional[list[int] | list[str] | int | str] = None, max_candidates: int = 60) -> Optional[date]
Picks a single real, "typical" date to run a "typical day" analysis on, when the caller has no specific date in mind.
Thin wrapper around representative_date.select_representative_date
-- see that function's docstring for the full algorithm. In short:
scores each candidate date by the MEDIAN, across parent_stations,
of the count of DISTINCT departure times serving that station that
day; groups dates into ISO weeks; scores each week by the MEDIAN of
its date_type-valid days' scores; picks the best-scoring week;
returns the day within it closest to that week's median score.
This replaced an older, weaker proxy that just counted active
service_ids per candidate date (Calendar.get_services_in_date),
which had no notion of whether a date's service was actually
full/typical and could be dominated by many small/seasonal
calendars while a metro area's dominant, highest-stop-density
agency had zero calendar coverage that day at all -- the root
cause of a real bug where most of Boston's and San Francisco's
transit stops silently vanished from stops.parquet (see
transitlos.stops.download_and_prepare_stops's module docstring).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
Optional[datetime | date]
|
Lower bound for candidate dates; defaults to
|
None
|
end_date
|
Optional[datetime | date]
|
Upper bound for candidate dates; defaults to
|
None
|
date_type
|
Optional[str]
|
|
'weekday'
|
route_types
|
Optional[list[int] | list[str] | int | str]
|
Optional route type filter -- if given, only trips of these route types are counted toward each station's distinct-departure-time score. |
None
|
max_candidates
|
int
|
Caps how many candidate ISO WEEKS are actually
evaluated (repurposed from the old per-day meaning, since
scoring is now vectorized over the whole window at once
rather than one |
60
|
Returns:
| Type | Description |
|---|---|
Optional[date]
|
The representative |
Optional[date]
|
no calendar date range at all (e.g. no |
Optional[date]
|
|
Optional[date]
|
some calendar coverage but zero real service, a best-effort |
Optional[date]
|
date is still returned rather than raising. |
Source code in pyGTFSHandler/feed.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
load ¶
load(gtfs_dirs: Union[List[Union[str, Path]], str, Path], aoi: Optional[Union[GeoDataFrame, GeoSeries]] = None, stop_group_distance: float = 0, start_date: Optional[datetime | date] = None, end_date: Optional[datetime | date] = None, date_type: Optional[list[str] | str] = None, start_time: Optional[datetime | time] = None, end_time: Optional[datetime | time] = None, route_types: Optional[list[int] | list[str] | int | str] = None, service_ids: Optional[List[str]] = None, trip_ids: Optional[List[str]] = None, stop_ids: Optional[List[str]] = None, route_ids: Optional[List[str]] = None, check_files: bool = True, min_file_id: int = 0)
Initializes a Feed instance by loading, filtering, and integrating GTFS data.
The constructor orchestrates the entire data loading pipeline, from reading individual files to joining them into a final, unified LazyFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gtfs_dirs
|
Union[List[Union[str, Path]], str, Path]
|
One or more paths to directories containing GTFS files. |
required |
aoi
|
Optional[Union[GeoDataFrame, GeoSeries]]
|
An Area of Interest (GeoDataFrame or GeoSeries) used to filter stops geospatially. Only stops within this AOI will be included. Defaults to None. |
None
|
service_ids
|
Optional[List[str]]
|
A list of |
None
|
trip_ids
|
Optional[List[str]]
|
A list of |
None
|
stop_ids
|
Optional[List[str]]
|
A list of |
None
|
route_ids
|
Optional[List[str]]
|
A list of |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any of the provided |
Source code in pyGTFSHandler/feed.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
load_shapes ¶
load_shapes(stops, stop_times, trips, gtfs_dir, use_real_shapes: bool = True, collision_registry=None, aoi: Optional[Union[GeoDataFrame, GeoSeries]] = None)
Builds self.shapes/self.trip_shape_ids_lf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_real_shapes
|
bool
|
When |
True
|
aoi
|
Optional[Union[GeoDataFrame, GeoSeries]]
|
Optional Area of Interest, the same one passed into
|
None
|
Source code in pyGTFSHandler/feed.py
build_lf ¶
build_lf(calendar: Calendar, routes: Optional[Routes], shapes: Shapes, stop_times: StopTimes, stops: Stops, trips: Trips, trip_shape_ids_lf: LazyFrame) -> pl.LazyFrame
Joins the loaded components into the single integrated self.lf.
Starts from stop_times.lf, left-joins frequencies (if present),
trips (service_id, route_id, direction_id), shape ids,
parent_station (deduplicating consecutive stops at the same
parent station within a trip), route metadata, and per-stop
shape geometry fields (shape_dist_traveled, shape_direction,
...). If stop_times.fixed_times is set, also runs the
shape-distance-based interpolation (_fix_null_times) for any
still-missing arrival/departure times. Finally flags each row with
isin_aoi based on the AOI-filtered stop set computed in load.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calendar
|
Calendar
|
Loaded |
required |
routes
|
Optional[Routes]
|
Loaded |
required |
shapes
|
Shapes
|
Loaded |
required |
stop_times
|
StopTimes
|
Loaded |
required |
stops
|
Stops
|
Loaded |
required |
trips
|
Trips
|
Loaded |
required |
trip_shape_ids_lf
|
LazyFrame
|
Mapping from synthetic |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
pl.LazyFrame: The denormalized schedule, one row per |
LazyFrame
|
(trip, stop) pair, assigned to |
Source code in pyGTFSHandler/feed.py
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 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 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 | |
concat_feeds ¶
Concatenate several already-loaded Feed objects into one.
Combines the calendar, routes, stop_times, stops and trips
components of every feed in feeds by concatenating their underlying
Polars/GeoPandas frames, then rebuilds shapes (load_shapes) and the
integrated lf (build_lf) on top of the combined data. Useful for
merging multiple regional GTFS feeds (e.g. one Feed per operator)
into a single queryable object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feeds
|
'Feed | list[Feed]'
|
A single |
required |
stop_group_distance
|
float
|
If greater than 0, stops within this distance
(in the stops' CRS units) are clustered together after
concatenation via |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
Feed |
'Feed'
|
The first feed in |
'Feed'
|
|
|
'Feed'
|
|
|
'Feed'
|
of every feed passed in. |
Source code in pyGTFSHandler/feed.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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 | |