在熊貓中一遍又一遍,我發現我必須劃分一段特定的時間。例如,對於時間序列中的每一天,請在每個中午12點將每個值除以值。Python熊貓時間系列比較具體時間
我覺得這應該是一個相當簡單的操作,但我沒有找到簡單的解決方案。
例如,我想在時間序列上每天執行一個功能:
x = df.groupby(df.index.date).apply(func)
每一天,做:
def func(df):
st = df.between_time('10:00','10:00')['y-value']
end = df.between_time('14:45','14:45')['y-value']
return (st/end)
首先,有沒有辦法說, df.at_time('10:00' )?在這裏寫betweenbetweenbetween()看起來很迂迴,但它起作用。我也嘗試df.index.time,但我不確定如何說,== datetime.time(10,0),因爲它返回一個布爾數組,而不僅僅是上午10點的值。
該函數不起作用,因爲我認爲這是一個索引問題,它會在每個值處吐出N/A併爲每一天吐出兩個值(即在10:00和14:45分別爲1 ), 不是一個。 如果它們是相同的時間,這會起作用,但如果它們是不同的時間則不起作用。我被引導認爲算術運算不能在不同的日期時間乾淨地工作。
我也試過:
def func(df):
st = df.reset_index().between_time('10:00','10:00')['mid'].values[0]
end = df.reset_index().between_time('14:45','14:45')['mid'].values[0]
return (st/end)
我得到一個索引錯誤說我需要返回DateTimeIndex。我認爲我不能只是將值分開,它會將該值返回到相應的日期,而是需要返回某種(索引,值)熊貓物體。
任何想法?這是一種常見的操作?
這裏是我的數據集的樣子(使用pd.read_clipboard()讀取):
bid ask mid
2000-01-01 12:00:00 288.0 289.5 288.75
2000-01-01 13:30:00 287.8 288.6 288.20000000000005
2000-01-01 14:00:00 287.75 289.25 288.5
2000-01-03 09:30:00 288.5 289.5 289.0
2000-01-03 10:15:00 288.5 289.5 289.0
2000-01-03 10:30:00 289.0 290.0 289.5
2000-01-03 10:45:00 288.75 289.75 289.25
2000-01-03 11:45:00 288.75 289.75 289.25
2000-01-03 13:00:00 288.5 289.5 289.0
2000-01-03 13:15:00 288.5 289.5 289.0
2000-01-03 13:30:00 288.5 289.5 289.0
2000-01-04 09:00:00 281.5 282.25 281.875
2000-01-04 09:15:00 281.0 281.5 281.25
2000-01-04 09:30:00 281.25 281.75 281.5
2000-01-04 09:45:00 281.1 281.85 281.475
2000-01-04 10:00:00 281.7 282.2 281.95
2000-01-04 10:30:00 282.0 282.75 282.375
2000-01-04 10:45:00 282.2 282.95 282.575
2000-01-04 11:15:00 282.3 282.8 282.55
2000-01-04 11:30:00 281.45 282.2 281.825
UPDATE:臨時的解決辦法,但我要找的東西清潔劑(它不存在,這是可能的)
st_time, end_time = '8:00', '14:45'
st, end = df.at_time(st_time), df.at_time(end_time)
AM = st.merge(end, on='date', how='left').dropna()
AM = AM.set_index(pd.DatetimeIndex(AM['date']))
AM['AM return'] = (AM[end_time]/AM[st_time]) - 1
AM = AM.rename(columns={'price_x': st_time+' price', 'price_y': end_time+' price'})
顯示您期待輸出的示例 – Jeff
您每天只有第一天的12點沒有價值。 –
嘗試返回'(st/end.values)' – HYRY