簡體   English   中英

Pandas groupby 給定的時間間隔

[英]Pandas groupby given time interval

我有一個間隔為 5 分鍾的 dataframe。 我想計算給定用戶指定的內部時間的回報。 例如,我想知道每天下午 2:55 到下午 3:00 的回報。 我如何對其執行分組操作?

我的數據看起來像這樣

                               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。

謝謝,

我的解決方案(假設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)

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)

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)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM