pd.rolling_max和min with groupby或类似

时间:2016-09-16 19:50:29

标签: python pandas

使用以下代码,我可以在H1_highH1_low列中捕获数据的滚动高低。

data["H1_high"] = pd.rolling_max(data.High, window=60, min_periods=1)
data["H1_low"] = pd.rolling_min(data.Low, window=60, min_periods=1)

这给出了以下输出:

                    Open    High    Low Last    Volume  H1_high H1_low
Timestamp                           
2014-03-04 09:30:00 1783.50 1784.50 1783.50 1784.50 171 1784.50 1783.5
2014-03-04 09:31:00 1784.75 1785.75 1784.50 1785.25 28  1785.75 1783.5
2014-03-04 09:32:00 1785.00 1786.50 1785.00 1786.50 81  1786.50 1783.5
2014-03-04 09:33:00 1786.00 1786.00 1785.25 1785.25 41  1786.50 1783.5
2014-03-04 09:34:00 1785.00 1785.25 1784.75 1785.25 11  1786.50 1783.5
2014-03-04 09:35:00 1785.50 1786.75 1785.50 1785.75 49  1786.75 1783.5
2014-03-04 09:36:00 1786.00 1786.00 1785.25 1785.75 12  1786.75 1783.5
2014-03-04 09:37:00 1786.00 1786.25 1785.25 1785.25 15  1786.75 1783.5

我想要做的只是在以下时间之间捕获H1_highH1_low

daystart = '9:30'
IB_end = '10:29:59'

IB_session = data.between_time(daystart,IB_end, include_start=True, include_end=True)

并每天执行此操作,显示H1_highH1_low然后将IB_end = '10:29:59'的最后一个值(FFill)结转到当天结束时(16:14:00) )。

所以这里是H1_high H1_low列的理想输出:

                    H1_high H1_low
2014-03-04 10:29:00 1786.75 1783.5
2014-03-04 10:30:00 1786.75 1783.5
2014-03-04 10:31:00 1786.75 1783.5

10:29:59的最终值向前填充到一天结束:

                    H1_high H1_low
2014-03-04 16:14:00 1786.75 1783.5

然后新的一天又重新开始:

                    H1_high H1_low
2014-03-05 09:30:00 1788.00 1783.00

1 个答案:

答案 0 :(得分:0)

使用datetime.time并过滤您的索引

import datetime

high_low_xt = lambda df: pd.concat([df.High.cummax(), df.Low.cummin()], axis=1)
tidx = pd.date_range('2016-09-14', '2016-09-16', freq='T')

start = datetime.time(9, 30)
end = datetime.time(10, 30)
eod = datetime.time(18, 14)

bidx = tidx[(tidx.time >= start) & (tidx.time < end)]
didx = tidx[(tidx.time >= start) & (tidx.time <= eod)]

df = pd.DataFrame(np.random.rand(len(tidx), 2), tidx, ['High', 'Low'])
df1 = df.ix[bidx].groupby(pd.TimeGrouper('D')).apply(high_low_xt)

df1.reindex(didx, method='ffill')

enter image description here

相关问题