Search code examples
pandastimedelta

Timedelta to "%H:%M" format


Is there a way to convert timedelta columns to strings in the "%H:%M" format? I need this even for timedeltas with more than 24 hours

I want to do this to make a .csv file that can be used in spreadsheets-like softwares (excel, google sheets...)


Solution

  • There's nothing built-in. You have to roll your own:

    s = pd.to_timedelta(['0:10:00', '12:34:56', '25:12:34']).to_series()
    s.dt.total_seconds().apply(lambda s: f'{s // 3600:02.0f}:{(s % 3600) // 60:02.0f}')
    

    Result:

    0 days 00:10:00    00:10
    0 days 12:34:56    12:34
    1 days 01:12:34    25:12
    dtype: object