Skip to content

BUG: Unexpected behavior of df.rolling(indexer).mean() #53603

Open
@philippesamuel

Description

@philippesamuel

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
from pandas.api.indexers import BaseIndexer

df = pd.DataFrame({
        'A': [10.0, 12.0, 14.0],
        'B': [20.0, 22.0, 24.0],
        'C': [30.0, 32.0, 34.0],
        'D': [40.0, 42.0, 44.0]
    })

expected = pd.DataFrame({
        'A': [ 5.0, 11.0, 12.0],
        'B': [10.0, 21.0, 22.0],
        'C': [15.0, 31.0, 32.0],
        'D': [20.0, 41.0, 42.0]
    })


class VariableWindowSizeIndexer(BaseIndexer):

    def __init__(self, window_sizes: Sequence[int], **kwargs) -> None:
        super(VariableWindowSizeIndexer, self).__init__(**kwargs)
        self.window_sizes = np.array(window_sizes, dtype='int64')

    def get_window_bounds(
            self,
            num_values: int = 0,
            min_periods: int | None = 1,
            center: bool | None = None,
            closed: str | None = None,
            step: int | None = None,
    ) -> tuple[np.ndarray, np.ndarray]:
        end = np.arange(len(self.window_sizes)) + 1
        start = end - self.window_sizes
        return start.astype('int64'), end.astype('int64')


window_sizes = [2, 2, 3]
indexer = VariableWindowSizeIndexer(window_sizes)
result = df.rolling(indexer).mean()
pd.testing.assert_frame_equal(result, expected)

Issue Description

result
A B C D
0 5.0 17.0 27.0 37.0
1 11.0 21.0 31.0 41.0
2 12.0 22.0 32.0 42.0

Expected Behavior

expected = pd.DataFrame({
'A': [ 5.0, 11.0, 12.0],
'B': [10.0, 21.0, 22.0],
'C': [15.0, 31.0, 32.0],
'D': [20.0, 41.0, 42.0]
})

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.11.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22000
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.5.3
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.8.0
pip : 23.0.1
Cython : None
pytest : 7.3.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds DiscussionRequires discussion from core team before further actionWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions