Python(Pandas)错误IndexError:单个位置索引器超出范围

时间:2018-01-26 00:53:55

标签: python python-3.x pandas runtime-error

这是我似乎无法压缩的错误,将我的计数减少到比实际行修复它少一个,但这意味着它甚至无法读取最后一行。该错误来自于我试图解析我保存在同一目录中的.csv中的数据。

以下是导致此问题的代码:

    margin1 = datetime.timedelta(days = 1)
    margin3 = datetime.timedelta(days = 3)
    margin7 = datetime.timedelta(days = 7)
    df = pd.read_csv('gameDB.csv')
    a = df.values
    rows=len(df.index)
    while (x <= rows):
        print (rows)
        print (x)
        input("Press Enter to continue...")
        csvName = str((df.iloc[x,0]))
        csvRel = str((df.iloc[x,1]))
        csvCal = str((df.iloc[x,2]))
        from datetime import datetime
        today = datetime.strptime(twiday, '%Y-%m-%d').date()
        compDate = datetime.strptime(csvRel, '%Y-%m-%d').date()
        print (csvName + ' ' + csvRel + ' ' + csvCal)
        try:
            if (today+margin7 == compDate):
                #tweet = (csvName + ' releases in 7 days. Click here to add to calendar ' + csvCal)
                #api.update_status(tweet)
                time.sleep(10)
            elif (today+margin3 == compDate):
                #tweet = (csvName + ' releases in 3 days. Click here to add to calendar ' + csvCal)
                #api.update_status(tweet)
                time.sleep(10)
            elif (today+margin1 == compDate):
                #tweet = (csvName + ' releases in tomorrow. Click here to add to calendar ' + csvCal)
                #api.update_status(tweet)
                time.sleep(10)
            elif (today == compDate):
                #tweet = (csvName + ' is now released.')
                #api.update_status(tweet)
                time.sleep(10)
        except:
            continue
        x += 1

这是我得到的错误

Traceback (most recent call last):
  File ".\gameRelease.py", line 306, in <module>
    NintendoSwitch()
  File ".\gameRelease.py", line 277, in NintendoSwitch
    main(system,data,color,calID)
  File ".\gameRelease.py", line 270, in main
    twitUpdate(tDay)
  File ".\gameRelease.py", line 97, in twitUpdate
    csvName = str((df.iloc[x,0]))
  File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1367, in __getitem__
    return self._getitem_tuple(key)
  File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1737, in _getitem_tuple
    self._has_valid_tuple(tup)
  File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 204, in _has_valid_tuple
    if not self._has_valid_type(k, i):
  File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1672, in _has_valid_type
    return self._is_valid_integer(key, axis)
  File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1713, in _is_valid_integer
    raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds

2 个答案:

答案 0 :(得分:0)

忘记在应用程序启动时创建csv时添加标题行,解决了所有问题。

    writer.writeheader()

这就是它所需要的一切。

答案 1 :(得分:0)

这只是在说您的iloc语句正在查找不存在的内容。如果您的DataFrame是5行长,则iloc [5,0]会超出范围。这是因为最后一行是iloc [4,0],因为它从0开始计数。