Skip to content

Commit 9c241fe

Browse files
wf-rwfr
and
wfr
authored
BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) (#47111)
* BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) * BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) fix tests, and add changes * BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) fix tests one more time * BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) make PEP8 happy * BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) make PEP8 happy * BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) fix tests one last time * Fixes from pre-commit [automated commit] * BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) requested changes * Fixes from pre-commit [automated commit] * BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) remove comments * BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) re-add gh ref * BUG Avoid IndexError on writing empty (row) MI df to excel (GH19543) fix changelog entry Co-authored-by: wfr <[email protected]>
1 parent 7397adc commit 9c241fe

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,7 @@ I/O
873873
- Bug in :func:`read_sas` returned ``None`` rather than an empty DataFrame for SAS7BDAT files with zero rows (:issue:`18198`)
874874
- Bug in :class:`StataWriter` where value labels were always written with default encoding (:issue:`46750`)
875875
- Bug in :class:`StataWriterUTF8` where some valid characters were removed from variable names (:issue:`47276`)
876+
- Bug in :meth:`DataFrame.to_excel` when writing an empty dataframe with :class:`MultiIndex` (:issue:`19543`)
876877

877878
Period
878879
^^^^^^

pandas/io/formats/excel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def _format_header_regular(self) -> Iterable[ExcelCell]:
634634
if self.index:
635635
coloffset = 1
636636
if isinstance(self.df.index, MultiIndex):
637-
coloffset = len(self.df.index[0])
637+
coloffset = len(self.df.index.names)
638638

639639
colnames = self.columns
640640
if self._has_aliases:

pandas/tests/io/excel/test_writers.py

+13
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,19 @@ def test_to_excel_multiindex_no_write_index(self, path):
838838
# Test that it is the same as the initial frame.
839839
tm.assert_frame_equal(frame1, frame3)
840840

841+
def test_to_excel_empty_multiindex(self, path):
842+
# GH 19543.
843+
expected = DataFrame([], columns=[0, 1, 2])
844+
845+
df = DataFrame([], index=MultiIndex.from_tuples([], names=[0, 1]), columns=[2])
846+
df.to_excel(path, "test1")
847+
848+
with ExcelFile(path) as reader:
849+
result = pd.read_excel(reader, sheet_name="test1")
850+
tm.assert_frame_equal(
851+
result, expected, check_index_type=False, check_dtype=False
852+
)
853+
841854
def test_to_excel_float_format(self, path):
842855
df = DataFrame(
843856
[[0.123456, 0.234567, 0.567567], [12.32112, 123123.2, 321321.2]],

0 commit comments

Comments
 (0)