Skip to main content
100%

A Python function to calculate the headways of transit vehicles arranged sequentially for a week, accounting for time over midnight over one or more days. Uses NumPy.

✓ Published0🌍 Public
Aalpha-beta-soup
Last edited May 8, 2014
Created on May 8, 2014

This example demonstrates a NumPy-based Python function that converts a week’s timetable of transit vehicle arrival times into headways—the seconds until the next vehicle arrives—while correctly handling gaps across midnight and entire days without service. The function `headway_array` takes a 2D array where rows represent sequential trips and columns represent days of the week, with `None` placeholders for trips that do not run on certain days. It returns an array of the same shape with headway values, using a custom formula to account for elapsed midnights and clamping results to a maximum of `maxseconds` times `daysinweek`. The code includes docstring examples showing the transformation on sample data.

AI-generated description

Similar vizzes