Pandas Rolling function
Last elements when window_size == step_size
I can't seem to get the last three element of an example 9 element series to be rolled on, when my window size and step size are both 3.
Is the below an intended behaviour of pandas?
My desired outcome
If so how can I roll over the Series so that:
pd.Series([1., 1., 1., 2., 2., 2., 3., 3., 3.]).rolling(window=3, step=3).mean()
evaluate to pd.Series([1., 2., 3.,])?
Example
import pandas as pd
def print_mean(x):
print(x)
return x.mean()
df = pd.DataFrame({"A": [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]})
df["left"] = (
df["A"].rolling(window=3, step=3, closed="left").apply(print_mean, raw=False)
)
df["right"] = (
df["A"].rolling(window=3, step=3, closed="right").apply(print_mean, raw=False)
)
df["both"] = (
df["A"].rolling(window=3, step=3, closed="both").apply(print_mean, raw=False)
)
df["neither"] = (
df["A"].rolling(window=3, step=3, closed="neither").apply(print_mean, raw=False)
)
This evaluates to:
A left right both neither
0 0.0 NaN NaN NaN NaN
1 1.0 NaN NaN NaN NaN
2 2.0 NaN NaN NaN NaN
3 3.0 1.0 2.0 1.5 NaN
4 4.0 NaN NaN NaN NaN
5 5.0 NaN NaN NaN NaN
6 6.0 4.0 5.0 4.5 NaN
7 7.0 NaN NaN NaN NaN
8 8.0 NaN NaN NaN NaN
and prints:
0 0.0
1 1.0
2 2.0
dtype: float64
3 3.0
4 4.0
5 5.0
dtype: float64
1 1.0
2 2.0
3 3.0
dtype: float64
4 4.0
5 5.0
6 6.0
dtype: float64
0 0.0
1 1.0
2 2.0
3 3.0
dtype: float64
3 3.0
4 4.0
5 5.0
6 6.0
dtype: float64
