我有一個多索引時間序列資料如下 -
庫存清單
datetimestamp,stock_id,period_value,date,stock_code,broker_token,period,open,high,low,close,volume,open_interest,candle_no
2021-10-25 09:15:00,AAA,1,2021-10-25,AAA,10100000007,1 min,1018.0,1018.9,1011.1,1014.25,5575,0.0,0
2021-10-25 09:16:00,AAA,1,2021-10-25,AAA,10100000007,1 min,1013.0,1013.0,1000.05,1002.6,9682,0.0,1
2021-10-25 09:17:00,AAA,1,2021-10-25,AAA,10100000007,1 min,1000.7,1001.3,994.0,995.0,13121,0.0,2
2021-10-25 09:18:00,AAA,1,2021-10-25,AAA,10100000007,1 min,994.7,997.65,994.0,995.2,5378,0.0,3
2021-10-25 09:19:00,AAA,1,2021-10-25,AAA,10100000007,1 min,995.3,998.4,995.15,998.0,3205,0.0,4
2021-10-25 09:16:00,BBB,1,2021-10-25,BBB,10100000005378,1 min,357.55,358.4,357.55,358.4,78,0.0,0
2021-10-25 09:17:00,BBB,1,2021-10-25,BBB,10100000005378,1 min,357.6,357.85,357.0,357.0,502,0.0,1
2021-10-25 09:18:00,BBB,1,2021-10-25,BBB,10100000005378,1 min,357.0,357.0,355.15,355.15,325,0.0,2
2021-10-25 09:19:00,BBB,1,2021-10-25,BBB,10100000005378,1 min,355.85,355.85,355.55,355.55,23,0.0,3
2021-10-25 09:20:00,BBB,1,2021-10-25,BBB,10100000005378,1 min,356.0,356.2,355.65,356.2,22,0.0,4
2021-10-25 09:21:00,BBB,1,2021-10-25,BBB,10100000005378,1 min,355.1,355.8,355.1,355.1,436,0.0,5
我正在嘗試從多索引資料幀中為每個時間戳在回圈中一個一個地讀取每只股票的開盤、代碼、交易量。
下面是代碼——
period_file_index = ['datetimestamp', 'stock_id', 'period_value']
period_file_df.set_index(period_file_index, inplace=True)
period_file_df = period_file_df.sort_index(level=[0,1,2])
index_slice = pd.IndexSlice
for stock in stock_list
open_price = period_file_df.loc[index_slice[('2021-10-25 09:15:00'), stock, 1]]['open']
我收到以下錯誤。
檔案
"/Users/macdev1/PycharmProjects/SMLogin2/library/utility/archive_data_analysis.py", line 218, in analyze_archive_dated_period_file_v0
open_price = period_file_df.loc[index_slice[('2021-10-25 09:15:00'), stock, 1]]['open']
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py", line 889, in __getitem__
return self._getitem_tuple(key)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py", line 1060, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py", line 799, in _getitem_lowerdim
return self._handle_lowerdim_multi_index_axis0(tup)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py", line 1088, in _handle_lowerdim_multi_index_axis0
raise ek
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py", line 1080, in _handle_lowerdim_multi_index_axis0
return self._get_label(tup, axis=axis)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py", line 1073, in _get_label
return self.obj.xs(label, axis=axis)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/generic.py", line 3733, in xs
loc, new_index = index._get_loc_level(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexes/multi.py", line 3038, in _get_loc_level
raise KeyError(key) from e
KeyError: ('2021-10-25 09:15:00', 'BBB', 1)
The reason is that there is no record for BBB for the datetimestamp - '2021-10-25 09:15:00' for period value 1.
我能夠通過為每個時間戳取最小值來克服錯誤,但這會增加額外的時間,因為必須為每只股票生成時間。
非常感謝有關如何捕獲此例外以及是否發生轉移到下一個股票的指導。
uj5u.com熱心網友回復:
使用try - except KeyError:
stock_list = ['AAA','BBB']
index_slice = pd.IndexSlice
for stock in stock_list:
try:
open_price = period_file_df.loc[index_slice[('2021-10-25 09:15:00'), stock, 1]]['open']
print (open_price)
except KeyError:
print ('keyerror')
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/358433.html
上一篇:如何根據列名合并資料框?
下一篇:如何使用行名稱創建資料框
