Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left-align a pandas rolling object

Tags:

python

pandas

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.

like image 629
Alex Avatar asked Jun 27 '16 13:06

Alex


People also ask

How to align two pandas DataFrames with each other?

In this tutorial, we will learn the python pandas DataFrame.align () method. This method aligns two objects on their axes with the specified join method. This method is helpful when we want to synchronize a dataframe with another dataframe or a dataframe with a Series using different join methods like the outer, inner, left, and right.

How rolling () function works in pandas Dataframe?

Pandas rolling | How rolling () Function works in Pandas Dataframe? Pandas rolling () function gives the element of moving window counts. The idea of moving window figuring is most essentially utilized in signal handling and time arrangement information.

Is there a way to center align a rolling object?

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: The standard method automatically aligns to the right, so there's no value at the first two indecies with a window of size three:

How to align text to the left in a Dataframe?

From the same example as the OP, one could left justify just the 'text' column: df = pd.DataFrame ( {'text': ['foo', 'bar'], 'number': [1, 2]}) dfStyler = df.style.set_properties (subset= ['text'],** {'text-align': 'left'}) If you wanna align both text and header to the left for example you can use:


2 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 
like image 153
jezrael Avatar answered Sep 21 '22 15:09

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.

like image 28
bluenote10 Avatar answered Sep 21 '22 15:09

bluenote10