运行代码时,出现错误IndexError:索引0超出了轴0的大小0。 我有一个具有两个输入参数的函数:“日期”和“符号”。
功能
def get_data(issue_date,stock_ticker):
df = pd.read_csv (r'D:\Project\Data\Short_Interest\mergedshort.csv')
df['Date'] = pd.to_datetime(df['Date'],format="%Y%m%d")
d = df
df = pd.DataFrame(d)
short = df.loc[df.Symbol.eq(stock_ticker)]
# get the index of the row of interest
ix = short[short.Date.eq(issue_date)].index[0]
# get the item row for that row's index
iloc_ix = short.index.get_loc(ix)
# get the +/-1 iloc rows (+2 because that is how slices work),basically +1 and -1 trading days
short_data = short.iloc[iloc_ix-10: iloc_ix+11]
return [short_data]
我导入一个csv文件,其中包含一堆带有输入参数的行:
df1 = pd.read_csv(r'C:\Users\L_\Documents\datatest2.csv')
df1['Date'] = pd.to_datetime(df1['Date'],format="%d/%m/%Y")
df1
看起来像这样(第一列:符号,第二列:日期):
Symbol Date
0 symbol1 date1
1 symbol2 date2
2 symbol3 date3
3 symbol4 date4
然后我想使用数据框行作为输入参数来调用该函数:
get_data(df1['Date'],df1['Symbol'])
但是,这样做时会出现IndexError。我尝试了多种方法来解决此问题,但是我陷入了困境。
完整堆栈跟踪:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-111-e33dfd079336> in <module>
----> 1 get_data(df1['Date'],df1['Symbol'])
<ipython-input-62-6cb8078d405b> in get_data(issue_date,stock_ticker)
11 short = df.loc[df.Symbol.eq(stock_ticker)]
12 # get the index of the row of interest
---> 13 ix = short[short.Date.eq(issue_date)].index[0]
14 # get the item row for that row's index
15 iloc_ix = short.index.get_loc(ix)
D:\Anaconda3\envs\analysis\lib\site-packages\pandas\core\indexes\base.py in __getitem__(self,key)
3956 if is_scalar(key):
3957 key = com.cast_scalar_indexer(key)
-> 3958 return getitem(key)
3959
3960 if isinstance(key,slice):
IndexError: index 0 is out of bounds for axis 0 with size 0