BUG: Error when exporting an empty DataFrame with MultiIndex to xlsx file · Issue #46873 · pandas-dev/pandas (original) (raw)

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [9], in <cell line: 7>()
      1 import pandas as pd
      3 df = pd.DataFrame(
      4     index=pd.MultiIndex.from_arrays([[], []], names=['a', 'b']),
      5     columns=['A', 'B']
      6 )
----> 7 df.to_excel('test.xlsx')

File E:\Program Files\Python39\lib\site-packages\pandas\core\generic.py:2345, in NDFrame.to_excel(self, excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow, startcol, engine, merge_cells, encoding, inf_rep, verbose, freeze_panes, storage_options)
   2332 from pandas.io.formats.excel import ExcelFormatter
   2334 formatter = ExcelFormatter(
   2335     df,
   2336     na_rep=na_rep,
   (...)
   2343     inf_rep=inf_rep,
   2344 )
-> 2345 formatter.write(
   2346     excel_writer,
   2347     sheet_name=sheet_name,
   2348     startrow=startrow,
   2349     startcol=startcol,
   2350     freeze_panes=freeze_panes,
   2351     engine=engine,
   2352     storage_options=storage_options,
   2353 )

File E:\Program Files\Python39\lib\site-packages\pandas\io\formats\excel.py:894, in ExcelFormatter.write(self, writer, sheet_name, startrow, startcol, freeze_panes, engine, storage_options)
    891     need_save = True
    893 try:
--> 894     writer.write_cells(
    895         formatted_cells,
    896         sheet_name,
    897         startrow=startrow,
    898         startcol=startcol,
    899         freeze_panes=freeze_panes,
    900     )
    901 finally:
    902     # make sure to close opened file handles
    903     if need_save:

File E:\Program Files\Python39\lib\site-packages\pandas\io\excel\_xlsxwriter.py:227, in XlsxWriter.write_cells(self, cells, sheet_name, startrow, startcol, freeze_panes)
    224 if validate_freeze_panes(freeze_panes):
    225     wks.freeze_panes(*(freeze_panes))
--> 227 for cell in cells:
    228     val, fmt = self._value_with_fmt(cell.val)
    230     stylekey = json.dumps(cell.style)

File E:\Program Files\Python39\lib\site-packages\pandas\io\formats\excel.py:831, in ExcelFormatter.get_formatted_cells(self)
    830 def get_formatted_cells(self) -> Iterable[ExcelCell]:
--> 831     for cell in itertools.chain(self._format_header(), self._format_body()):
    832         cell.val = self._format_value(cell.val)
    833         yield cell

File E:\Program Files\Python39\lib\site-packages\pandas\io\formats\excel.py:630, in ExcelFormatter._format_header_regular(self)
    628     coloffset = 1
    629     if isinstance(self.df.index, MultiIndex):
--> 630         coloffset = len(self.df.index[0])
    632 colnames = self.columns
    633 if self._has_aliases:

File E:\Program Files\Python39\lib\site-packages\pandas\core\indexes\multi.py:2075, in MultiIndex.__getitem__(self, key)
   2073 retval = []
   2074 for lev, level_codes in zip(self.levels, self.codes):
-> 2075     if level_codes[key] == -1:
   2076         retval.append(np.nan)
   2077     else:

IndexError: index 0 is out of bounds for axis 0 with size 0