seemo seemo - 5 months ago 264
Python Question

Pandas Groupby TimeGrouper and apply

As per this question. This groupby works when applied to my

df
for a
pd.rolling_mean
column as follows:

data['maFast']=data['Last'].groupby(pd.TimeGrouper('d'))
.apply(pd.rolling_mean,center=False,win‌​dow=10)


How do I apply the same groupby logic to another element of my
df
which contains
pd.rolling_std
and
pd.rolling_mean
:

data['maSlow_std'] = pd.rolling_mean(data['Last'], window=60) +
2* pd.rolling_std(data['Last'], 20, min_periods=20)

Answer

I think you need function lambda:

data['maSlow_std']=data['Last'].groupby(pd.TimeGrouper('d'))
                               .apply(lambda x: pd.rolling_mean(x, window=60) + 
                                             2* pd.rolling_std(x, 20, min_periods=20))

If pandas version 0.18.0+:

data['maSlow_std1']=data['Last'].groupby(pd.TimeGrouper('d'))
                                .apply(lambda x: x.rolling(window=60,center=False).mean() + 
                                              2* x.rolling(window=20,min_periods=20).std()) 

Sample with window=10 and min_periods=5:

data['maSlow_std1']=data['Last'].groupby(pd.TimeGrouper('d'))
                                .apply(lambda x: x.rolling(window=10,center=False).mean() + 
                                              2* x.rolling(window=10,min_periods=5).std()) 

print (data)

                        Open     High      Low     Last  Volume  maSlow_std1
Timestamp                                                                   
2014-03-04 09:30:00  1783.50  1784.50  1783.50  1784.50     171          NaN
2014-03-04 09:31:00  1784.75  1785.75  1784.50  1785.25      28          NaN
2014-03-04 09:32:00  1785.00  1786.50  1785.00  1786.50      81          NaN
2014-03-04 09:33:00  1786.00  1786.00  1785.25  1785.25      41          NaN
2014-03-04 09:34:00  1785.00  1785.25  1784.75  1785.25      11          NaN
2014-03-04 09:35:00  1785.50  1786.75  1785.50  1785.75      49          NaN
2014-03-04 09:36:00  1786.00  1786.00  1785.25  1785.75      12          NaN
2014-03-04 09:37:00  1786.00  1786.25  1785.25  1785.25      15          NaN
2014-03-04 09:38:00  1785.50  1785.50  1784.75  1785.25      24          NaN
2014-03-04 09:39:00  1785.50  1786.00  1785.25  1785.25      13  1786.432796
2014-03-04 09:40:00  1786.00  1786.25  1783.50  1783.75      28  1786.700379
2014-03-04 09:41:00  1784.00  1785.00  1784.00  1784.25      12  1786.760687
2014-03-04 09:42:00  1784.25  1784.75  1784.00  1784.25      18  1786.354006
2014-03-04 09:43:00  1784.75  1785.00  1784.50  1784.50      10  1786.300379
2014-03-04 09:44:00  1784.25  1784.25  1783.75  1784.00      32  1786.268181
2014-03-04 09:45:00  1784.50  1784.75  1784.50  1784.75      11  1786.008094
2014-03-04 09:46:00  1785.00  1785.00  1784.50  1784.50      11  1785.656409
2014-03-04 09:47:00  1785.00  1785.75  1784.75  1785.75      20  1785.877775
2014-03-04 09:48:00  1785.75  1786.00  1785.75  1786.00      17  1786.186981
2014-03-04 09:49:00  1786.00  1786.50  1785.75  1786.00      13  1786.449150
2014-03-04 09:50:00  1786.50  1788.75  1786.25  1788.50     307  1787.988613
2014-03-04 09:51:00  1788.25  1788.25  1787.75  1787.75      17  1788.647768
2014-03-04 09:52:00  1787.75  1787.75  1787.25  1787.25      11  1788.947768
2014-03-04 09:53:00  1787.25  1787.50  1787.25  1787.25      11  1789.156890
2014-03-04 09:54:00  1787.00  1787.50  1786.75  1786.75      26  1789.019047
2014-03-04 09:55:00  1787.25  1788.25  1787.25  1788.00      11  1789.206849
Comments