[Python] Polars - rolling - How to reset count each day?

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I've switched from Pandas-Python to Polars and I'm trying to figure out how to essentially do a rolling sum for each day. I don't want just the total for each day, I want the totals for each time period (5m period) each day, in a "rolling_sum" fashion. Here is the code I have so far:

    from datetime import datetime, timedelta

    df = pl.DataFrame({
    "date": pl.datetime_range(
    datetime(1985, 1, 1),
    datetime(1985, 1, 6),
    timedelta(minutes=5),
    time_unit="ns",
    eager=True
    )
    })

    df = df.with_columns(
    A=pl.lit(2, dtype=pl.Int32)
    )

    df = (
    df.lazy()
    .rolling("date", period="1d")
    .agg(
    pl.col("A").sum().alias("A_daily")
    )
    .collect()
    )

    print(df[280:])


    This produces:

    shape: (1_161, 2)
    ┌─────────────────────┬─────────┐
    │ date ┆ A_daily │
    │ --- ┆ --- │
    │ datetime[ns] ┆ i32 │
    ╞═════════════════════╪═════════╡
    │ 1985-01-01 23:20:00 ┆ 562 │
    │ 1985-01-01 23:25:00 ┆ 564 │
    │ 1985-01-01 23:30:00 ┆ 566 │
    │ 1985-01-01 23:35:00 ┆ 568 │
    │ 1985-01-01 23:40:00 ┆ 570 │
    │ 1985-01-01 23:45:00 ┆ 572 │
    │ 1985-01-01 23:50:00 ┆ 574 │
    │ 1985-01-01 23:55:00 ┆ 576 │
    │ 1985-01-02 00:00:00 ┆ 576 │
    │ 1985-01-02 00:05:00 ┆ 576 │
    │ 1985-01-02 00:10:00 ┆ 576 │
    │ 1985-01-02 00:15:00 ┆ 576 │
    │ 1985-01-02 00:20:00 ┆ 576 │
    │ 1985-01-02 00:25:00 ┆ 576 │
    │ 1985-01-02 00:30:00 ┆ 576 │
    │ 1985-01-02 00:35:00 ┆ 576 │
    │ 1985-01-02 00:40:00 ┆ 576 │
    │ 1985-01-02 00:45:00 ┆ 576 │
    │ 1985-01-02 00:50:00 ┆ 576 │
    ...
    │ 1985-01-05 23:50:00 ┆ 576 │
    │ 1985-01-05 23:55:00 ┆ 576 │
    │ 1985-01-06 00:00:00 ┆ 576 │
    └─────────────────────┴─────────┘


    What I guess what I'm actually after is for each day, I want the A_Daily to reset, i.e. to look like:

    shape: (1_161, 2)
    ┌─────────────────────┬─────────┐
    │ date ┆ A_daily │
    │ --- ┆ --- │
    │ datetime[ns] ┆ i32 │
    ╞═════════════════════╪═════════╡
    │ 1985-01-01 23:20:00 ┆ 562 │
    │ 1985-01-01 23:25:00 ┆ 564 │
    │ 1985-01-01 23:30:00 ┆ 566 │
    │ 1985-01-01 23:35:00 ┆ 568 │
    │ 1985-01-01 23:40:00 ┆ 570 │
    │ 1985-01-01 23:45:00 ┆ 572 │
    │ 1985-01-01 23:50:00 ┆ 574 │
    │ 1985-01-01 23:55:00 ┆ 576 │
    │ 1985-01-02 00:00:00 ┆ 2 │ # !!!! CHANGE HERE !!!!
    │ 1985-01-02 00:05:00 ┆ 4 │
    │ 1985-01-02 00:10:00 ┆ 6 │
    │ 1985-01-02 00:15:00 ┆ 8 │
    │ 1985-01-02 00:20:00 ┆ 10 │
    │ 1985-01-02 00:25:00 ┆ 12 │
    │ 1985-01-02 00:30:00 ┆ 14 │
    │ 1985-01-02 00:35:00 ┆ 18 │
    │ 1985-01-02 00:40:00 ┆ 20 │
    │ 1985-01-02 00:45:00 ┆ 22 │
    │ 1985-01-02 00:50:00 ┆ 24 │



    Basically, I want to just reset the rolling sum each day. In Pandas, I'd have done it sort of like this:

    tn = outdata.groupby(pd.Grouper(freq='D', key='_daily_reset'))[
    # '_vp_volume'].transform('cumsum')


    Thanks!

    Continue reading...

Compartilhe esta Página