[Python] Calculate cumulative sum of time series X for time points in series Y

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    Imagine transactions, identified by amount, arriving throughout the day. You want to calculate the running total of amount at given points in time (9 am, 10 am, etc.).

    With pandas, I would use apply to perform such an operation. With Polars, I tried using map_elements. I have also considered group_by_dynamic but I am not sure it gives me control of the time grid's start / end / increment.

    Is there a better way?

    import polars as pl
    import datetime

    df = pl.DataFrame({
    "time": [
    datetime.datetime(2025, 2, 2, 11, 1),
    datetime.datetime(2025, 2, 2, 11, 2),
    datetime.datetime(2025, 2, 2, 11, 3)
    ],
    "amount": [5.0, -1, 10]
    })

    dg = pl.DataFrame(
    pl.datetime_range(
    datetime.datetime(2025, 2, 2, 11, 0),
    datetime.datetime(2025, 2, 2, 11, 5),
    "1m",
    eager = True
    ),
    schema=["time"]
    )

    def _cumsum(dt):
    return df.filter(pl.col("time") <= dt).select(pl.col("amount")).sum().item()

    dg.with_columns(
    cum_amount=pl.col("time").map_elements(_cumsum, return_dtype= pl.Float64)
    )

    Continue reading...

Compartilhe esta Página