我的解决方案(假设 Close 列用于返回计算)。
编辑:由于价格系列每五分钟连续记录一次,因此不确定您所说的 groupby 是什么意思。 Groupby 不同股票的开始时间为 14.55?
import pandas as pd
ind = pd.date_range('2019-07-12 14:40', periods=5, freq='5min')
stock = {'Close':[0.411891,0.412942,0.412680,0.413992,0.412942]}
df = pd.DataFrame(data=stock,index=ind)
print(df)
output = df.between_time('14:55','15:00')['Close'].pct_change().iloc[1]
print('Return-----')
print(output)
输出
Close
2019-07-12 14:40:00 0.411891
2019-07-12 14:45:00 0.412942
2019-07-12 14:50:00 0.412680
2019-07-12 14:55:00 0.413992
2019-07-12 15:00:00 0.412942
Return-----
-0.0025362808943169
Edit2:我现在明白你的意思了。试试这个。
import pandas as pd
ind = pd.date_range('2019-07-12 14:40', periods=5, freq='5min')
ind = ind.append(pd.date_range('2019-07-13 14:40', periods=5, freq='5min'))
stock = {'Close':[0.411891,0.412942,0.412680,0.413992,0.412942,0.423567,0.456321,0.465789,0.431900,0.431672]}
df = pd.DataFrame(data=stock,index=ind)
df.rename_axis('date',inplace=True)
df['date_'] = df.index.date
print(df)
print(df.between_time('14:55','15:00')['Close'])
output = df.between_time('14:55','15:00').groupby('date_').pct_change().iloc[1::2]
print('Return-----')
print(output)
输出
Return-----
Close
date
2019-07-12 15:00:00 -0.002536
2019-07-13 15:00:00 -0.000528
Edit3:这个新代码对于天数重叠和超过 5 分钟的返回非常有效。我以 15 分钟返回为例(23:55-00:05)。
import pandas as pd
ind = pd.date_range('2019-07-12 23:50', periods=5, freq='5min')
ind = ind.append(pd.date_range('2019-07-13 23:50', periods=5, freq='5min'))
stock = {'Close':[0.411891,0.412942,0.412680,0.413992,0.412942,0.423567,0.456321,0.465789,0.431900,0.431672]}
df = pd.DataFrame(data=stock,index=ind)
df.rename_axis('date',inplace=True)
print(df)
# I am setting this manually, you may improve it by writing a function that calculates
# the number of five-minute interval
num =2
ind = df.between_time('23:55','00:05').iloc[::num+1]['Close'].index
old_price = df.between_time('23:55','00:05')['Close'].iloc[::num+1].reset_index(drop=True)
new_price = df.between_time('23:55','00:05')['Close'].iloc[num::num+1].reset_index(drop=True)
#print(old_price)
#print(new_price)
output = pd.DataFrame(new_price/old_price-1)
output.set_index(ind,inplace=True)
print('Return-----')
print(output)
输出
Close
date
2019-07-12 23:50:00 0.411891
2019-07-12 23:55:00 0.412942
2019-07-13 00:00:00 0.412680
2019-07-13 00:05:00 0.413992
2019-07-13 00:10:00 0.412942
2019-07-13 23:50:00 0.423567
2019-07-13 23:55:00 0.456321
2019-07-14 00:00:00 0.465789
2019-07-14 00:05:00 0.431900
2019-07-14 00:10:00 0.431672
Return-----
Close
date
2019-07-12 23:55:00 0.002543
2019-07-13 23:55:00 -0.053517