簡體   English   中英

Python (Pandas) 錯誤 IndexError:單個位置索引器越界

[英]Python (Pandas) Error IndexError: single positional indexer is out-of-bounds

這是我似乎無法消除的錯誤,將我的計數降低到比實際行少一個修復它,但這意味着它甚至無法讀取最后一行。 錯誤來自我試圖解析我保存在同一目錄中的 .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

在應用程序啟動時創建 csv 時忘記添加標題行,這解決了所有問題。

    writer.writeheader()

這就是它所需要的。

這只是說您的 iloc 語句之一正在查找不存在的內容。 如果您的 DataFrame 有 5 行長,則 iloc[5, 0] 會超出范圍。 這是因為最后一行是 iloc[4, 0],因為它從 0 開始計數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM