Module: GtfsDf::Utils
Constant Summary collapse
- SECONDS_IN_MINUTE =
60- SECONDS_IN_HOUR =
SECONDS_IN_MINUTE * 60
- SECONDS_IN_DAY =
SECONDS_IN_HOUR * 24
Instance Method Summary collapse
-
#as_seconds_since_midnight(col_name) ⇒ Object
Converts a GTFS time string column to seconds since midnight.
-
#as_time_string(col_name) ⇒ Object
Converts a seconds since midnight column to GTFS time string (HH:MM:SS).
-
#inspect_time(series) ⇒ Object
Converts a seconds since midnight Series to GTFS time strings for inspection.
-
#parse_date(col) ⇒ Object
Parses a GTFS date string.
Instance Method Details
#as_seconds_since_midnight(col_name) ⇒ Object
Converts a GTFS time string column to seconds since midnight
Use this method with Polars DataFrames to convert time columns.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gtfs_df/utils.rb', line 17 def as_seconds_since_midnight(col_name) parts = Polars.col(col_name).str.split(":") hours = parts.list.get(0).cast(Polars::Int64) minutes = parts.list.get(1).cast(Polars::Int64) seconds = parts.list.get(2).cast(Polars::Int64) (hours * SECONDS_IN_HOUR) + (minutes * SECONDS_IN_MINUTE) + seconds end |
#as_time_string(col_name) ⇒ Object
Converts a seconds since midnight column to GTFS time string (HH:MM:SS)
Use this method with Polars DataFrames to convert time columns back to strings.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gtfs_df/utils.rb', line 37 def as_time_string(col_name) total_seconds = Polars.col(col_name) hours = total_seconds.floordiv(SECONDS_IN_HOUR) minutes = (total_seconds % SECONDS_IN_HOUR).floordiv(SECONDS_IN_MINUTE) seconds = total_seconds % SECONDS_IN_MINUTE Polars.format( "{}:{}:{}", hours.cast(Polars::String).str.zfill(2), minutes.cast(Polars::String).str.zfill(2), seconds.cast(Polars::String).str.zfill(2) ) end |
#inspect_time(series) ⇒ Object
Converts a seconds since midnight Series to GTFS time strings for inspection
Use this method to get a readable view of time columns during debugging. It’s not meant to be performant.
60 61 62 63 64 |
# File 'lib/gtfs_df/utils.rb', line 60 def inspect_time(series) series.to_frame.with_columns( as_time_string(series.name) )[series.name] end |
#parse_date(col) ⇒ Object
Parses a GTFS date string
The input string is expected to be a service day in the YYYYMMDD format. Since time within a service day may be above 24:00:00, a service day may contain information for the subsequent day(s).
75 76 77 |
# File 'lib/gtfs_df/utils.rb', line 75 def parse_date(col) col.str.strptime(Polars::Date, "%Y%m%d", strict: false) end |