Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(python): Add example for index count in DataFrame.rolling #16600

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5719,6 +5719,27 @@ def rolling(
│ 2020-01-03 19:45:32 ┆ 11 ┆ 2 ┆ 9 │
│ 2020-01-08 23:16:43 ┆ 1 ┆ 1 ┆ 1 │
└─────────────────────┴───────┴───────┴───────┘

If you use an index count in `period` or `offset`, then it's based on the
values in `index_column`:

>>> df = pl.DataFrame({"int": [0, 4, 5, 6, 8], "value": [1, 4, 2, 4, 1]})
>>> df.rolling("int", period="3i").agg(pl.col("int").alias("aggregated"))
shape: (5, 2)
┌─────┬────────────┐
│ int ┆ aggregated │
│ --- ┆ --- │
│ i64 ┆ list[i64] │
╞═════╪════════════╡
│ 0 ┆ [0] │
│ 4 ┆ [4] │
│ 5 ┆ [4, 5] │
│ 6 ┆ [4, 5, 6] │
│ 8 ┆ [6, 8] │
└─────┴────────────┘

If you want the index count to be based on row number, then you may want to
combine `rolling` with :meth:`.with_row_index`.
"""
period = deprecate_saturating(period)
offset = deprecate_saturating(offset)
Expand Down