Pandas 0.22.0:IndexError:读取xls

时间:2018-02-07 16:56:11

标签: excel pandas python-3.6

我正在尝试将282Mb(65536行x 138列).xls文件加载到pandas数据帧中

import pandas as pd
import os

filename = r'invoicing.xls'
dir = os.path.dirname(os.path.abspath(filename))
excelFile = os.path.join(dir, filename)
invoicing_info = pd.read_excel(excelFile)

但我得到了这个

Traceback (most recent call last):
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/xlrd/sheet.py", line 698, in put_cell_unragged
    self._cell_types[rowx][colx] = ctype
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/juanda/PycharmProjects/Hyperion_info/load_info.py", line 11, in <module>
    invoicing_info = pd.read_excel(excelFile, sheet_name=0)
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/pandas/util/_decorators.py", line 118, in wrapper
    return func(*args, **kwargs)
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/pandas/io/excel.py", line 230, in read_excel
    io = ExcelFile(io, engine=engine)
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/pandas/io/excel.py", line 294, in __init__
    self.book = xlrd.open_workbook(self._io)
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/xlrd/__init__.py", line 162, in open_workbook
    ragged_rows=ragged_rows,
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/xlrd/book.py", line 107, in open_workbook_xls
    bk.fake_globals_get_sheet()
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/xlrd/book.py", line 728, in fake_globals_get_sheet
    self.get_sheets()
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/xlrd/book.py", line 719, in get_sheets
    self.get_sheet(sheetno)
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/xlrd/book.py", line 710, in get_sheet
    sh.read(self)
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/xlrd/sheet.py", line 1361, in read
    self_put_cell(rowx, colx, None, d, self.fixed_BIFF2_xfindex(cell_attr, rowx, colx))
  File "/Users/juanda/conda_envs/Hyperion_contracts_env/lib/python3.6/site-packages/xlrd/sheet.py", line 709, in put_cell_unragged
    assert 1 <= nr <= self.utter_max_rows
AssertionError

我认为这是.xls扩展名的问题,但我无法在上传之前修改该文件。如何以可靠的方式上传此.xls文件?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。在将“仅值”复制并粘贴到新工作表中之后,将工作表移动(工作表的顺序),现在可以正常工作了。这很烦人。