pandas python索引越界错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21004221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:31:44  来源:igfitidea点击:

python index out of bounds error

pythonlistpandas

提问by Biggie Gv0

Tryind this code:

试试这个代码:

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

Gives the index out of bounds error. Sample of symb_list:

给出索引越界错误。symb_list 示例:

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

earnings_vola(i) returns float

收益_vola(i) 返回浮动

UPD. Sorry, im new user here.

更新。对不起,我是这里的新用户。

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

UPD2

UPD2

> 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

**UPD3 Example of result earnings_vola(symbol) function:

**UPD3 结果earning_vola(symbol) 函数示例:

earnings_vola(symbol='MSFT')
0.080011249349832989**

I need to iterate list of symbols (example above) and get all results in list

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

回答by abarnert

The problem comes from this line:

问题来自这一行:

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

And it's complaining that df['timetype']is a sequence with only 0, 1, or 2 items, but you're asking for the third.

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

This could be a silly typo of the kind we all make, where you accidentally wrote 2instead of 1, or forgot that Python sequences use 0-based indices.

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

But if not, if you really were expecting df['timetype']to have 3 or more values and it doesn't, we need to know what values you expected it to have, and why, and what values it actuallyhas.

但如果不是,如果您确实希望df['timetype']有 3 个或更多值而它没有,我们需要知道您希望它具有哪些值,为什么,以及它实际具有哪些值。

You can get started on debugging this by just adding a line to your code and running again:

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

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

If the result isn't what you expected, try printing out dfitself, or dor h, etc., until you find the first place where it goes wrong.

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

At some point, you'll find the first step that's returning a different value than you expected. You may still have no idea whyit's returning a different value—but at that point, you've got a much easier question for StackOverflow to answer.

在某些时候,您会发现第一步返回的值与您预期的不同。您可能仍然不知道为什么它会返回不同的值——但此时,您有一个更容易的问题让 StackOverflow 来回答。

回答by Ammar Hasan

Change this code

更改此代码

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

to this code (explicit, correct and readable)

到此代码(明确、正确和可读)

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