[Python] Interpretation of nan in rolling_mean with polars

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

  1. Stack

    Stack Membro Participativo

    How can we do to do a rolling mean with ignoring the np.nan values that would be in it. From what I read on the documentation, I don t get if it is not possible at all or if some tricky parameters must be passed. If it is not implemented at all, it would be a great idea to add it because it prevents from doing commands such as "do the rolling mean on the N previous row, and ignoring the rows that does not follow a certain condition in those previous N rows".

    Here is a quick example to show my problem/question.

    import polars as pl
    import numpy as np
    test = np.array([1, 2, np.nan, 3, 4, np.nan]).reshape(-1, 1)
    test = pl.DataFrame(test, schema=["a"])
    test.with_columns(rolling_mean=pl.col("a").rolling_mean(window_size=2))


    shape: (6, 2)
    ┌─────┬──────────────┐
    │ a ┆ rolling_mean │
    │ --- ┆ --- │
    │ f64 ┆ f64 │
    ╞═════╪══════════════╡
    │ 1.0 ┆ null │
    │ 2.0 ┆ 1.5 │
    │ NaN ┆ NaN │
    │ 3.0 ┆ NaN │
    │ 4.0 ┆ 3.5 │
    │ NaN ┆ NaN │
    └─────┴──────────────┘

    Continue reading...

Compartilhe esta Página