AssertionError when calling pands read_excel on xls file using xlrd engine

Viewed 624

I'm trying to read in an excel file (.xls) via pandas into a data frame as follows:

df = pd.read_excel(
        filename, sheet_name='Sheet1', nrows=6)

Unfortunately I get an AssertionError. However if I open the file in excel and then click save, then rerun this works perfectly fine. I've not changed any data, just opened in Excel and save. Has anyone come across this issue before?

This is the assertion error I'm getting:

AssertionError                            Traceback (most recent call last)
<ipython-input-153-58dcba1b45c3> in <module>
      1 df = pd.read_excel(
----> 2             filename, sheet_name='Sheet1', nrows=6)

~\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    206                 else:
    207                     kwargs[new_arg_name] = new_arg_value
--> 208             return func(*args, **kwargs)
    209 
    210         return wrapper

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, verbose, parse_dates, date_parser, thousands, comment, skip_footer, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    308 
    309     if not isinstance(io, ExcelFile):
--> 310         io = ExcelFile(io, engine=engine)
    311     elif engine and engine != io.engine:
    312         raise ValueError(

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in __init__(self, io, engine)
    817         self._io = _stringify_path(io)
    818 
--> 819         self._reader = self._engines[engine](self._io)
    820 
    821     def __fspath__(self):

~\Anaconda3\lib\site-packages\pandas\io\excel\_xlrd.py in __init__(self, filepath_or_buffer)
     19         err_msg = "Install xlrd >= 1.0.0 for Excel support"
     20         import_optional_dependency("xlrd", extra=err_msg)
---> 21         super().__init__(filepath_or_buffer)
     22 
     23     @property

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in __init__(self, filepath_or_buffer)
    357             self.book = self.load_workbook(filepath_or_buffer)
    358         elif isinstance(filepath_or_buffer, str):
--> 359             self.book = self.load_workbook(filepath_or_buffer)
    360         else:
    361             raise ValueError(

~\Anaconda3\lib\site-packages\pandas\io\excel\_xlrd.py in load_workbook(self, filepath_or_buffer)
     34             return open_workbook(file_contents=data)
     35         else:
---> 36             return open_workbook(filepath_or_buffer)
     37 
     38     @property

~\Anaconda3\lib\site-packages\xlrd\__init__.py in open_workbook(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows)
    155         formatting_info=formatting_info,
    156         on_demand=on_demand,
--> 157         ragged_rows=ragged_rows,
    158     )
    159     return bk

~\Anaconda3\lib\site-packages\xlrd\book.py in open_workbook_xls(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows)
    118             bk._sheet_list = [None for sh in bk._sheet_names]
    119             if not on_demand:
--> 120                 bk.get_sheets()
    121         bk.nsheets = len(bk._sheet_list)
    122         if biff_version == 45 and bk.nsheets > 1:

~\Anaconda3\lib\site-packages\xlrd\book.py in get_sheets(self)
    721         for sheetno in xrange(len(self._sheet_names)):
    722             if DEBUG: print("GET_SHEETS: sheetno =", sheetno, self._sheet_names, self._sh_abs_posn, file=self.logfile)
--> 723             self.get_sheet(sheetno)
    724 
    725     def fake_globals_get_sheet(self): # for BIFF 4.0 and earlier

~\Anaconda3\lib\site-packages\xlrd\book.py in get_sheet(self, sh_number, update_pos)
    712             sh_number,
    713         )
--> 714         sh.read(self)
    715         self._sheet_list[sh_number] = sh
    716         return sh

~\Anaconda3\lib\site-packages\xlrd\sheet.py in read(self, bk)
   1107                     saved_obj_id = None
   1108             elif rc == XL_NOTE:
-> 1109                 self.handle_note(data, txos)
   1110             elif rc == XL_FEAT11:
   1111                 self.handle_feat11(data)

~\Anaconda3\lib\site-packages\xlrd\sheet.py in handle_note(self, data, txos)
   1985         # string length).
   1986         # Issue 4 on github: Google Spreadsheet doesn't write the undefined byte.
-> 1987         assert (data_len - endpos) in (0, 1)
   1988         if OBJ_MSO_DEBUG:
   1989             o.dump(self.logfile, header="=== Note ===", footer= " ")

AssertionError: 
1 Answers

This often happens if you have a date column in your data that hasn't been set to 'Date' format. Set Date columns to 'Date' in excel and the error will disappear.

Related