[Python] Python Polars: Calculate time difference from first element in each (repeating) group

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I have a polars.DataFrame like:

    df = pl.DataFrame({
    "timestamp": ['2009-04-18 11:30:00', '2009-04-18 11:40:00', '2009-04-18 11:50:00', '2009-04-18 12:00:00', '2009-04-18 12:10:00', '2009-04-18 12:20:00', '2009-04-18 12:30:00'],
    "group": ["group_1", "group_1", "group_1", "group_2", "group_2", "group_1", "group_1"]})

    df = df.with_columns(
    pl.col("timestamp").str.to_datetime().dt.replace_time_zone("UTC"),
    )

    ┌─────────────────────────┬─────────┐
    │ timestamp ┆ group │
    │ --- ┆ --- │
    │ datetime[μs, UTC] ┆ str │
    ╞═════════════════════════╪═════════╡
    │ 2009-04-18 11:30:00 UTC ┆ group_1 │
    │ 2009-04-18 11:40:00 UTC ┆ group_1 │
    │ 2009-04-18 11:50:00 UTC ┆ group_1 │
    │ 2009-04-18 12:00:00 UTC ┆ group_2 │
    │ 2009-04-18 12:10:00 UTC ┆ group_2 │
    │ 2009-04-18 12:20:00 UTC ┆ group_1 │ <- reappearance of group_1
    │ 2009-04-18 12:30:00 UTC ┆ group_1 │ <- reappearance of group_1
    └─────────────────────────┴─────────┘


    I want to calculate the time difference between the timestamp of the first element in each group to the timestamp of the elements in a group. Important is, that 'group' is defined as a (chronologically) consecutive appearance of the same group label. Like in the example shown group labels can occur later in time with the same group label but should by then be treated as a new group.

    With that, the result should look something like this:

    ┌─────────────────────────┬─────────┬─────────┐
    │ timestamp ┆ group │ timediff│
    │ --- ┆ --- │ --- │
    │ datetime[μs, UTC] ┆ str │ int(?) │
    ╞═════════════════════════╪═════════╪═════════╡
    │ 2009-04-18 11:30:00 UTC ┆ group_1 │ 0 │
    │ 2009-04-18 11:40:00 UTC ┆ group_1 │ 10 │
    │ 2009-04-18 11:50:00 UTC ┆ group_1 │ 20 │
    │ 2009-04-18 12:00:00 UTC ┆ group_2 │ 0 │
    │ 2009-04-18 12:10:00 UTC ┆ group_2 │ 10 │
    │ 2009-04-18 12:20:00 UTC ┆ group_1 │ 0 │ <- reappearance of group_1
    │ 2009-04-18 12:30:00 UTC ┆ group_1 │ 10 │ <- reappearance of group_1
    └─────────────────────────┴─────────┴─────────┘

    Continue reading...

Compartilhe esta Página