Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExcelFile parse of empty sheet fails with "IndexError: list index out of range" #6403

Closed
myourshaw opened this issue Feb 19, 2014 · 2 comments · Fixed by #10376
Closed

ExcelFile parse of empty sheet fails with "IndexError: list index out of range" #6403

myourshaw opened this issue Feb 19, 2014 · 2 comments · Fixed by #10376
Labels
Bug IO Excel read_excel, to_excel
Milestone

Comments

@myourshaw
Copy link

Using pandas 0.13.1 on OS X Mavericks to parse a blank Excel spreadsheet causes "IndexError: list index out of range". Apparently the default header=0 in _parse_excel causes the execution of _trim_excel_header(data[header]). Perhaps when nrows==0 this should not be executed.

import pandas as pd
xl_file = pd.ExcelFile('blank.xlsx')
xl_file.parse('Sheet1') #Sheet1 has no data

STDERR:

Traceback (most recent call last):
  File "/Users/myourshaw/lab/pypeline/python2/excel_example.py", line 10, in <module>
    xl_file.parse('Sheet1')
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/excel.py", line 208, in parse
    **kwds)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/excel.py", line 291, in _parse_excel
    data[header] = _trim_excel_header(data[header])
IndexError: list index out of range
@jreback jreback added this to the 0.15.0 milestone May 16, 2014
@Ander150
Copy link

Ander150 commented Jul 3, 2014

FYI ... I still get an error if I bypass "_trim_excel_header(data[header])". I'm able to get around the problem if I return out of _parse_excel() when no rows are detected.
...

     if isinstance(sheetname, compat.string_types):
        sheet = self.book.sheet_by_name(sheetname)
    else:  # assume an integer if not a string
        sheet = self.book.sheet_by_index(sheetname)


    if not sheet.nrows:
        return 

    data = []
    should_parse = {}

...

The terminal output looks like this ...

import pandas as pd
xl_file=pd.ExcelFile('blank.xlsx')
xl_file.parse('Sheet1')

@jetheurer
Copy link

I'm also having trouble with this on Windows 7

IndexError                                Traceback (most recent call last)
<ipython-input-10-cff728300bb3> in <module>()
     22     if not len(power):
     23         print 'error - no data: %s'%installationid, ' only %s days'
---> 24         writer.save()
     25         continue
     26     if not 'Production' in power.columns:

C:\Anaconda\lib\site-packages\pandas\io\excel.pyc in save(self)
   1100         Save workbook to disk.
   1101         """
-> 1102         return self.book.save(self.path)
   1103 
   1104     def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):

C:\Anaconda\lib\site-packages\xlwt\Workbook.pyc in save(self, filename)
    660 
    661         doc = CompoundDoc.XlsDoc()
--> 662         doc.save(filename, self.get_biff_data())
    663 
    664 

C:\Anaconda\lib\site-packages\xlwt\Workbook.pyc in get_biff_data(self)
    641         eof = self.__eof_rec()
    642 
--> 643         self.__worksheets[self.__active_sheet].selected = True
    644         sheets = ''
    645         sheet_biff_lens = []

IndexError: list index out of range

Is there a solution?

@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
@jreback jreback modified the milestones: 0.17.0, Next Major Release Jun 18, 2015
bashtage pushed a commit to bashtage/pandas that referenced this issue Jun 20, 2015
Add support for StringIO/BytesIO to ExcelWriter
Add vbench support for writing excel files
Add support for serializing lists/dicts to strings
Fix bug when reading blank excel sheets
Added xlwt to Python 3.4 builds

closes pandas-dev#8188
closes pandas-dev#7074
closes pandas-dev#6403
closes pandas-dev#7171
closes pandas-dev#6947
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants