Module: GtfsDf::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/gtfs_df/utils.rb

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

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.

Examples:

dataframe.with_columns(GtfsDf::Utils.as_seconds_since_midnight(‘start_time’))

Parameters:

  • col_name

    String The column to convert

Returns:

  • Polars::Expr



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.

Examples:

dataframe.with_columns(GtfsDf::Utils.as_time_string(‘start_time’))

Parameters:

  • col_name

    String The column to convert

Returns:

  • Polars::Expr



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.

Parameters:

  • series

    Polars::Series The series to convert

Returns:

  • Polars::Series A series with time strings



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).

Examples:

20180913 for September 13th, 2018.

Parameters:

  • col

    Polars::Expr



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