Pandas roll_sum centered and min_periods

I would like to use the function pandas.rolling_sumfor DataFrameto summarize the window using any data available for each window (so do not return NaNwhen the window goes 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 main, centered, moving amount ...

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 exclude values NaNand use any data available in each window. My guess was that the option would min_periodstake care of that ...

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 with center=True, but I am confused why the last three values ​​are missing. I expected the last three values ​​to be ...

# 2014-01-06  10
# 2014-01-07   9
# 2014-01-08   8

- , min_periods , center=True ? ?

+3
1

, pandas :

>>> 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]

shift -offset,

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

NaN , min_periods=0; , :

>>> 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]
+2

All Articles