Skip to content

Rolling mean / sum incorrect behavior when min_samples < window #22868

@szev77

Description

@szev77

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

pl.DataFrame({"a": [1, 2, 3, 4, 5]}).with_columns(
    pl.col("a").rolling_mean(2, weights=[1, 2], min_samples=1)
)

Log output

shape: (5, 1)
┌──────────┐
│ a        │
│ ---      │
│ f64      │
╞══════════╡
│ 0.333333 │
│ 1.666667 │
│ 2.666667 │
│ 3.666667 │
│ 4.666667 │
└──────────┘

Issue description

For rolling functions with weights supplied, when min_samples is set smaller than the window size, then the left most values of weights are applied in calculations to the sub-windows, when in fact it should be the right-most.

Expected behavior

In the example above, the first value should either be 1 (as the mean over on sample point is the sample point itsefl), or 0.6666... if we normalize by the total sum of weights. Having the option for both outputs is useful fwiw.

Installed versions

adbc_driver_manager  <not installed>
altair               <not installed>
azure.identity       <not installed>
boto3                <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
google.auth          2.23.3
great_tables         <not installed>
matplotlib           3.8.0
numpy                1.26.0
openpyxl             3.1.4
pandas               2.2.3
polars_cloud         <not installed>
pyarrow              19.0.1
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           2.0.0
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds triageAwaiting prioritization by a maintainerpythonRelated to Python Polars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions