Skip to content

Commit

Permalink
BUG: Fix .to_excel() for MultiIndex containing a NaN value #13511 (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
mpuels authored and jorisvandenbossche committed Jul 25, 2016
1 parent 2166ac1 commit 4c2840e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,5 @@ Bug Fixes
- Bug where ``pd.read_gbq()`` could throw ``ImportError: No module named discovery`` as a result of a naming conflict with another python package called apiclient (:issue:`13454`)
- Bug in ``Index.union`` returns an incorrect result with a named empty index (:issue:`13432`)
- Bugs in ``Index.difference`` and ``DataFrame.join`` raise in Python3 when using mixed-integer indexes (:issue:`13432`, :issue:`12814`)

- Bug in ``.to_excel()`` when DataFrame contains a MultiIndex which contains a label with a NaN value (:issue:`13511`)
6 changes: 5 additions & 1 deletion pandas/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,11 @@ def _format_hierarchical_rows(self):
for spans, levels, labels in zip(level_lengths,
self.df.index.levels,
self.df.index.labels):
values = levels.take(labels)

values = levels.take(labels,
allow_fill=levels._can_hold_na,
fill_value=True)

for i in spans:
if spans[i] > 1:
yield ExcelCell(self.rowcounter + i, gcolidx,
Expand Down
14 changes: 14 additions & 0 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,20 @@ def test_to_excel_multiindex(self):
parse_dates=False)
tm.assert_frame_equal(frame, df)

# GH13511
def test_to_excel_multiindex_nan_label(self):
_skip_if_no_xlrd()

frame = pd.DataFrame({'A': [None, 2, 3],
'B': [10, 20, 30],
'C': np.random.sample(3)})
frame = frame.set_index(['A', 'B'])

with ensure_clean(self.ext) as path:
frame.to_excel(path, merge_cells=self.merge_cells)
df = read_excel(path, index_col=[0, 1])
tm.assert_frame_equal(frame, df)

# Test for Issue 11328. If column indices are integers, make
# sure they are handled correctly for either setting of
# merge_cells
Expand Down

0 comments on commit 4c2840e

Please sign in to comment.