python索引越界

2024-09-26 04:57:31 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试此代码:

f2 = []
for i in symb_list: 
    f2.append(earnings_vola(i))

给出索引越界错误。符号列表示例:

symb_list
Out[143]:
['MTMC',
 'ANCI',
 'TPLM',
 'BERK',
 'DGI',
 'QLTY',
 'GST',
 'AGEN',
 'NURO',

收益率浮动

升级版。抱歉,我是新用户。

def earnings_vola (symbol):
    price_b = marketdata.ext.load_metotron('%s'%symbol)
    price = price_b.sort()
    d = pickle.load(open('/home/mad/Appr/data_%s.pickle'%(symbol), 'rb'))
    df = h.to_df(d)
    if df['timetype'][2]=='After Close':
        price['VOLA'] = (price.shift(-1)['C']-price['C'])/price['C']*100
    else:
        price['VOLA'] = (price['C']-price.shift(+1)['C'])/price['C']*100
    x1 = pa.Series(sorted(df['Date_p']))
    px = price.reindex(x1, method='ffill')
    avg_vola = np.mean(px['VOLA'])
    return avg_vola

上浮2

> IndexError                                Traceback (most recent call
> last) <ipython-input-144-f3de6042c223> in <module>()
>       1 f2 = []
>       2 for i in symb_list:
> ----> 3     f2.append(earnings_vola(i))
> 
> <ipython-input-123-96f164ec1ad9> in earnings_vola(symbol)
>       4     d = pickle.load(open('/home/mad/Appr/data_%s.pickle'%(symbol), 'rb'))
>       5     df = h.to_df(d)
> ----> 6     if df['timetype'][2]=='After Close':
>       7         price['VOLA'] = (price.shift(-1)['C']-price['C'])/price['C']*100
>       8     else:
> 
> /usr/local/lib/python2.7/dist-packages/pandas/core/series.pyc in
> __getitem__(self, key)
>     616     def __getitem__(self, key):
>     617         try:
> --> 618             return self.index.get_value(self, key)
>     619         except InvalidIndexError:
>     620             pass
> 
> /usr/local/lib/python2.7/dist-packages/pandas/core/index.pyc in
> get_value(self, series, key)
>     728 
>     729             try:
> --> 730                 return tslib.get_value_box(series, key)
>     731             except IndexError:
>     732                 raise
> 
> /usr/local/lib/python2.7/dist-packages/pandas/tslib.so in
> pandas.tslib.get_value_box (pandas/tslib.c:8534)()
> 
> /usr/local/lib/python2.7/dist-packages/pandas/tslib.so in
> pandas.tslib.get_value_box (pandas/tslib.c:8378)()
> 
> IndexError: index out of bounds

**向上3 结果收益(符号)函数示例:

earnings_vola(symbol='MSFT')
0.080011249349832989**

我需要迭代符号列表(上面的例子)并得到列表中的所有结果


Tags: keyinselfpandasdfgetvaluesymbol
2条回答

问题就出在这一行:

if df['timetype'][2]=='After Close':

它抱怨说df['timetype']是一个只有0、1或2个项的序列,但您要求的是第三个。

这可能是一个我们都会犯的愚蠢的错误,你不小心写了2,而不是1,或者忘记了Python序列使用基于0的索引。

但如果不是,如果你真的期望df['timetype']有3个或更多的值,而它没有,我们需要知道你期望它有什么值,为什么,以及它实际上有什么值。

只需在代码中添加一行并再次运行,就可以开始调试:

    df = h.to_df(d)
    print(df['timetype']) # NEW CODE
    if df['timetype'][2]=='After Close':

如果结果不是您所期望的,请尝试打印df本身,或dh等,直到找到第一个出错的地方。

在某个时刻,您会发现第一步返回的值与预期的不同。您可能仍然不知道为什么返回不同的值,但在这一点上,您有一个更容易回答的问题StackOverflow。

更改此代码

if df['timetype'][2]=='After Close':
    price['VOLA'] = (price.shift(-1)['C']-price['C'])/price['C']*100

此代码(显式、正确和可读)

key = 'timetype'
value = df[key] if df and key in df else None
kw = value[2] if value and len(value) > 2 and value[2]=='After Close' else None
if kw:
    price['VOLA'] = (price.shift(-1)['C']-price['C'])/price['C']*100

相关问题 更多 >