熊猫按给定的时间间隔分组

时间:2019-10-03 03:11:29

标签: python pandas

我有一个5分钟间隔的数据帧。我想根据用户指定的内部时间来计算收益。例如,我想知道每天从2:55 PM到3:00 PM的收益。我该如何对其进行分组操作?

我的数据看起来像这样

                               Open      High       Low     Close  Volume           VB           VS     MCVol         MCVal   OpenInt    Ret  TotalRet
date                                                                                                                                                  
2019-07-12 14:40:00+08:00  0.411629  0.412154  0.411366  0.411891  2412.0  408971474.0  414685176.0  315290.0  3.007625e+10  556102.0   25.0   41975.0
2019-07-12 14:45:00+08:00  0.411891  0.413205  0.411629  0.412942  6536.0  408975390.0  414687662.0  318884.0  3.041836e+10  556200.0  100.0   42075.0
2019-07-12 14:50:00+08:00  0.412942  0.413205  0.411891  0.412680  3288.0  408976658.0  414689656.0  320962.0  3.061613e+10  555638.0  -25.0   42050.0
2019-07-12 14:55:00+08:00  0.412680  0.414254  0.412417  0.413992  5926.0  408980236.0  414691758.0  324190.0  3.092359e+10  555482.0  125.0   42175.0
2019-07-12 15:00:00+08:00  0.413729  0.413992  0.412417  0.412942  8190.0  408983450.0  414696208.0  329278.0  3.140824e+10  553480.0 -100.0   42075.0

大多数问题是通过重新采样解决的,我不确定是否适合我的情况。我的更多是使用between_time()和groupby的组合。

谢谢

1 个答案:

答案 0 :(得分:1)

我的解决方案(假设关闭列用于退货计算)。 编辑:虽然价格序列每五分钟连续记录一次,但是不确定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