Skip to content

Commit 1e4e380

Browse files
code sample for pandas-dev#43267
1 parent 79b3d45 commit 1e4e380

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

bisect/43267.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# BUG: crash in df.groupby.rolling.mean with forward window indexer #43267
2+
3+
import numpy as np
4+
import pandas as pd
5+
6+
print(pd.__version__)
7+
8+
df = pd.DataFrame(
9+
{"B": [np.nan, 1, 2, np.nan, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]}
10+
)
11+
df["A"] = 1
12+
indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=12) # forward 12 period
13+
14+
# First time is OK
15+
df["mean"] = (
16+
df.groupby("A")["B"]
17+
.rolling(window=indexer, min_periods=1)
18+
.mean()
19+
.reset_index()
20+
.loc[:, "B"]
21+
)
22+
23+
# Second time would crash!
24+
df["mean"] = (
25+
df.groupby("A")["B"]
26+
.rolling(window=indexer, min_periods=1)
27+
.mean()
28+
.reset_index()
29+
.loc[:, "B"]
30+
)
31+
print(df)

0 commit comments

Comments
 (0)