[Python] Computing time delta between two columns in Polars

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am struggling to find a good way to compute time delta in Polars. I have the following string data set and would like to compute the duration in hours between start and end. The tricky part is working around midnight.

    df = pl.DataFrame(
    {
    'start_time': [
    '23:00',
    '00:00'
    ],
    'end_time': [
    '24:00',
    '01:00'
    ]
    }
    )

    (
    df
    .with_columns(
    start_time = pl.col('start_time').str.to_time('%H:%M'),
    end_time = pl.col('end_time').replace('24:00', '00:00').str.to_time('%H:%M')
    )
    .with_columns(
    duration = (
    pl.when(pl.col('end_time') == pl.time(0, 0, 0))
    .then(86400000000000)
    .otherwise(pl.col('end_time').cast(pl.Int64))
    - pl.col('start_time').cast(pl.Int64)) / 3600000000000
    )

    )


    I came up with a solution, but I am very unhappy with it, because it feels unnecessarily complicated. Basically I am substituting end times equal to 24:00 with 00:00, because Polars can't work with the value 24:00. Because of this I have to add a when/then/otherwise to again substitute the 00:00 in the end times with the time in nanoseconds which equal 24:00. Then I am able to cast the times to int, compute the difference in nanoseconds and convert back to hours by dividing through nanoseconds per hour.

    Continue reading...

Compartilhe esta Página