[Python] How should I parse times in the Japanese "30-hour" format for data analysis?

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I'm considering a data analysis project involving information on Japanese TV broadcasts. The relevant data will include broadcast times, and some of those will be for programs that aired late at night.

    Late-night Japanese TV schedules follow a non-standard time format called the 30-hour system (brief English explanation here). Most times are given in normal Japan Standard Time, formatted as %H:%M. Times from midnight to 6 AM, however, are treated as an extension of the previous day and numbered accordingly, under the logic that that's how people staying up late experience them. For example, Macross Frontier was broadcast in Kansai at 1:25 AM, but it was written as 25:25.

    I want to use this data in a Pandas or Polars DataFrame. Theoretically, it could be left as a string, but it'd be more useful to convert it to a standard format for datetimes -- either Python's built-in type, or the types used in NumPy or Polars. One simple approach could be:

    from datetime import date, time, datetime
    from zoneinfo import ZoneInfo

    def process_30hour(d: date, t: str):
    h, m = [int(n) for n in t.split(':')] # assumes format 'HH:MM' for t
    if h > 23:
    h -= 24
    d += 1
    return datetime.combine(d, time(h, m), ZoneInfo('Japan'))


    This could then be applied to a whole DataFrame with DataFrame.apply(). There may be a more performant way, however, especially considering the vectorization features of DataFrames -- both libraries recommend avoiding DataFrame.apply() if there's an alternative.

    Continue reading...

Compartilhe esta Página