pandas rolling_sum with center and min_periods

pandas, python

Solution

it seems that pandas first calculates:

>>> pd.rolling_sum(df, 7, center=False, min_periods=0)
            num
2014-01-01    1
2014-01-02    2
2014-01-03    3
2014-01-04    4
2014-01-05    6
2014-01-06    8
2014-01-07   10
2014-01-08   11

[8 rows x 1 columns]

and then `shift`s the result by `-offset`, where

offset = int((window - 1) / 2.)

this causes `NaN` values for the last entries even though `min_periods=0`; a work around my be as below:

>>> rs = pd.rolling_sum(df, 7, center=True, min_periods=0)
>>> rs.update( pd.rolling_sum(df.iloc[:-7:-1], 7, center=True, min_periods=0) )
>>> rs
            num
2014-01-01    4
2014-01-02    6
2014-01-03    8
2014-01-04   10
2014-01-05   11
2014-01-06   10
2014-01-07    9
2014-01-08    8

[8 rows x 1 columns]

Problem

I would like to use the `pandas.rolling_sum` function on a `DataFrame` to sum over a window using whatever data are available for each window (so don't return `NaN` when the window extends beyond the available data). Here are some sample data: ``` import pandas as pd # version 0.12.0 (Python 2.7) df = pd.DataFrame([1]*4+[2]*4, index=pd.date_range('2014-1-1', periods=8, freq='D'), columns=['num']) df.head() # num # 2014-01-01 1 # 2014-01-02 1 # 2014-01-03 1 # 2014-01-04 1 # 2014-01-05 2 ``` Here is the basic, centered rolling sum... ``` pd.rolling_sum(df, 7, center=True) # num # 2014-01-01 NaN # 2014-01-02 NaN # 2014-01-03 NaN # 2014-01-04 10 # 2014-01-05 11 # 2014-01-06 NaN # 2014-01-07 NaN # 2014-01-08 NaN ``` I want to eliminate the `NaN` values and use whatever data are available within each window. My hunch was that the `min_periods` option would take care of this... ``` pd.rolling_sum(df, 7, center=True, min_periods=0) # num # 2014-01-01 4 # 2014-01-02 6 # 2014-01-03 8 # 2014-01-04 10 # 2014-01-05 11 # 2014-01-06 NaN # 2014-01-07 NaN # 2014-01-08 NaN ``` This works when the window is not centered using `center=True`, but I'm confused why the last three values are missing. I was expecting the last three values to be... ``` # 2014-01-06 10 # 2014-01-07 9 # 2014-01-08 8 ``` Can anyone explain why `min_periods` is working on the first observations but failing on the last observations when using `center=True` option? What's the fix?

Original source