Left-align a pandas rolling object

Question:

Using pandas 0.18.1, I’d like to take the rolling average of a one-column dataframe. Since version 0.18.0, this is done with rolling() objects. The default for these rolling objects is to be right-justified. There is a boolean argument you can pass, center=True, to align the rolling object to the center value, but there doesn’t seem to be a way to left-align it. Here’s an example:

df = pandas.DataFrame({'A': [2,3,6,8,20, 27]})
df
    A
0   2
1   3
2   6
3   8
4  20
5  27

The standard method automatically aligns to the right, so there’s no value at the first two indecies with a window of size three:

 df.rolling(window=3).mean()
           A
0        NaN
1        NaN
2   3.666667
3   5.666667
4  11.333333
5  18.333333

We can center-align the operation like this:

df.rolling(window=3).mean(center=True)
           A
0        NaN
1   3.666667
2   5.666667
3  11.333333
4  18.333333
5        NaN

But what I’m looking for is this:

df.rolling(3).mean()
            A
 0   3.666667
 1   5.666667
 2  11.333333
 3  18.333333
 4        NaN
 5        NaN

I can accomplish this by doing it with the default right alignment, and then re-indexing it, or by reversing the order of the rows and then doing it “right-aligned” but these are work-arounds for what should be a straight-forward operation.

Asked By: Alex

||

Answers:

I think you can use shift:

a = df.rolling(window=3).mean().shift(-2)
print (a)
           A
0   3.666667
1   5.666667
2  11.333333
3  18.333333
4        NaN
5        NaN
Answered By: jezrael

Another solution is to simply reverse the DataFrame/Series before applying the right-aligned rolling window, and re-reverse it afterwards. Something like:

In [1]: df["A"][::-1].rolling(3).mean()[::-1]
Out[1]: 
0     3.666667
1     5.666667
2    11.333333
3    18.333333
4          NaN
5          NaN
Name: A, dtype: float64

The benefit over shift is that it should work with variable sized windows in case of time-based windows.

Answered By: bluenote10
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.