-
-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Description
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 numpy as np
import pandas as pd
# Create sample data
dates = pd.date_range(start='2024-01-01', periods=5, freq='D')
ids = range(1, 3)
np.random.seed(42)
# Create all combinations of dates and ids
data = []
for date in dates:
for id in ids:
data.append({
'date': date,
'Id': id,
'value': np.random.rand() * 100
})
df_sample = pd.DataFrame(data)
print(df_sample)
def rolling_func(x):
print(x)
return x.iloc[-1]
df_sample.set_index('date').groupby('Id')['value'].rolling(5, min_periods=1).apply(rolling_func, raw=False)Issue Description
date Id value
2024-01-01 1 37.454012
2024-01-01 2 95.071431
2024-01-02 1 73.199394
2024-01-02 2 59.865848
2024-01-03 1 15.601864
2024-01-03 2 15.599452
2024-01-04 1 5.808361
2024-01-04 2 86.617615
2024-01-05 1 60.111501
2024-01-05 2 70.807258
For a sample dataframe like the above, the code returns the following results. I believe that the rolling results based on the group (by Id) and time-based index (by date) are wrong.
date
2024-01-01 37.454012
dtype: float64
date
2024-01-01 37.454012
2024-01-01 73.199394
dtype: float64
date
2024-01-01 37.454012
2024-01-01 73.199394
2024-01-02 15.601864
dtype: float64
date
2024-01-01 37.454012
2024-01-01 73.199394
2024-01-02 15.601864
2024-01-02 5.808361
dtype: float64
date
2024-01-01 37.454012
2024-01-01 73.199394
2024-01-02 15.601864
2024-01-02 5.808361
2024-01-03 60.111501
dtype: float64
date
2024-01-03 95.071431
dtype: float64
date
2024-01-03 95.071431
2024-01-04 59.865848
dtype: float64
date
2024-01-03 95.071431
2024-01-04 59.865848
2024-01-04 15.599452
dtype: float64
date
2024-01-03 95.071431
2024-01-04 59.865848
2024-01-04 15.599452
2024-01-05 86.617615
dtype: float64
date
2024-01-03 95.071431
2024-01-04 59.865848
2024-01-04 15.599452
2024-01-05 86.617615
2024-01-05 70.807258
dtype: float64
Expected Behavior
I expect the results shall be like the following:
date
2024-01-01 37.454012
dtype: float64
date
2024-01-01 37.454012
2024-01-02 73.199394 # Instead of "2024-01-01 73.199394"
dtype: float64
date
2024-01-01 37.454012
2024-01-02 73.199394 # Instead of "2024-01-01 73.199394"
2024-01-03 15.601864 # Instead of "2024-01-02 15.601864"
dtype: float64
date
2024-01-01 37.454012
2024-01-02 73.199394 # Instead of "2024-01-01 73.199394"
2024-01-03 15.601864 # Instead of "2024-01-02 15.601864"
2024-01-04 5.808361 # Instead of "2024-01-02 5.808361"
Installed Versions
Details
INSTALLED VERSIONS
commit : 9c8bc3e
python : 3.13.11
python-bits : 64
OS : Darwin
OS-release : 24.6.0
Version : Darwin Kernel Version 24.6.0: Wed Nov 5 21:28:03 PST 2025; root:xnu-11417.140.69.705.2~1/RELEASE_ARM64_T8122
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.3.3
numpy : 2.2.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.2
Cython : 3.1.2
sphinx : None
IPython : 9.1.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.5
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : 2024.11.0
fsspec : 2025.5.1
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : None
matplotlib : 3.10.7
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.16.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None