Pandas time group rolling functions

here is my problem. I have a DataFrame as follows:

df:

2013-10-24      1
2013-10-25      2
2013-11-27      3 
2013-11-28      4
2013-12-01      5 
2013-12-02      6

I want the DataFrame to look like this:

roll_mean (df, window = '1M'):

2013-10      1.5
2013-11      3.5
2013-12      5.5 

roll_mean (df, window = '2M'):

2013-10      NAN
2013-11      2.5
2013-12      4.5 

roll_mean (df, window = '3M'):

2013-10      NAN
2013-11      NAN
2013-12      3.5 

roll_mean (df, window = '1Y'):

2013-10      NAN
2013-11      NAN
2013-12      NAN

where 1M is "1 month", 2M is "2 months". The window is not an int value, but a time interval such as "1D", "3M", "1Y", etc. This function can be grouped using a data block using a unit of time, such as "D", "M", "Y", and then rolling the data frame through a number to a unit of time, such as 1, 3 ...

Do I need a rolling function like this? Can anyone help me out? Did I give a clear description? Many thanks.

Update:

. ​​, , , .

, , , :

2013-10-24      1
2013-10-25      2
2013-11-27      3 
2013-11-28      4
2013-12-01      5 
2013-12-02      6

pd.rolling_std (df.resample( '1 '), = 1):

2013-10    NAN
2013-11    NAN 
2013-12    NAN

, dataframe, (window = 1):

2013-10    0.5
2013-11    0.5 
2013-12    0.5

0.5 - , np.sqrt([1,2]) . 0,5 [3,4] [5,6]. , how = 'xxx' resample, . 2 ,

df (window = 2):

2013-10    NAN
2013-11    1.1180 
2013-12    1.1180

1.1180 - , np.sqrt([1,2,3,4]) . 1.1180 2013-12 [3,4,5,6] 2013-11 2013-12 .

p.s. - , ... ~

+4
1

to_datetime datetimeindex.

df = pd.DataFrame({'value': [1, 2, 3, 4, 5, 6]},
                  index=['2013-10-24', '2013-10-25', '2013-11-27', 
                         '2013-11-28', '2013-12-01', '2013-12-02'])           
df.index = pd.to_datetime(df.index)

>>> pd.rolling_mean(df.resample('1M'), 1, freq='1M')
            value
2013-10-31    1.5
2013-11-30    3.5
2013-12-31    5.5

>>> pd.rolling_mean(df.resample('2M'), window=1, freq='1M')
            value
2013-10-31    1.5
2013-11-30    NaN
2013-12-31    4.5

>>> pd.rolling_mean(df.resample('1M'), window=2, freq='1M')
            value
2013-10-31    NaN
2013-11-30    2.5
2013-12-31    4.5

>>> pd.rolling_mean(df.resample('1M'), window=3, freq='1M')
            value
2013-10-31    NaN
2013-11-30    NaN
2013-12-31    3.5

>>> pd.rolling_mean(df.resample('1M'), window=12, freq='1M')
            value
2013-10-31    NaN
2013-11-30    NaN
2013-12-31    NaN
+2

Source: https://habr.com/ru/post/1613643/


All Articles