Python for Transit: Line frequencies in a map from GTFS

Dive deeper into gtfs_functions Python package

In this article, we will see how to get line frequencies from a GTFS using the Python package gtfs_functions. You can find the repository and official documentation on GitHub.

If you are looking for an extensive explanation of the package, I recommend you first read this introduction. Here, we are going to directly dive into the specific use case of getting stop frequencies in a map.

To install the package and parse the GTFS run the code below. For the article, I downloaded the GTFS from SFMTA (San Francisco, CA).

The function lines_freq takes 5 arguments:

  • stop_times calculated in step 1
  • trips calculated in step 1
  • shapes calculated in step 1
  • routes calculated in step 1
  • cutoffs: list of numbers that define the time windows we want to aggregate the data by.

The output for one specific line shows:

GeoDataFrame output for the function lines_freq().

Which has the following columns:

  • route_id from the GTFS
  • route_name
  • dir_id: the direction is “Inbound” if it had 0 in the GTFS and “Outbound” if it had a 1.
  • window: service window defined from the “cutoffs” input.
  • frequency: hourly frequency in minutes per trip in the window.
  • ntrips: number of trips in the widow.
  • max_freq: highest hourly frequency in the day in minutes per trip.
  • max_trips: maximum number of hourly trips that take place in that stop.
  • geometry

You can always export the GeoDataFrames we saw and open them in your favorite GIS software, but I added a function to allow the user to quickly take a look from the notebook before going into that workflow. It is not meant to be presentation-ready or fully customizable, just to take a quick look.

The function map_gdf() is built on top of the folium library and allows you to quickly visualize and style the data on a map.

It takes 6 arguments as shown below. For example, to visualize line frequencies:

Acknowledgments & References

Far from taking credit from other’s work, I want to acknowledge that some functions of this package were built on top of great and more generic packages and were just slightly modified to better serve this specific workflow.

For example, the function import_gtfs() heavily relies on partridge, a powerful Python library created by Remix founders that makes parsing a GTFS very easy. Similarly, map_gdf() and save_gdf() are built on top of folium and geopandas respectively.

Sr. Data Analyst at Remix.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store