Skip to content

BUG: Fix for MultiIndex to_excel() with index=False. #5659

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

Merged
merged 1 commit into from
Dec 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ def _format_header_mi(self):
coloffset = 0
lnum = 0

if isinstance(self.df.index, MultiIndex):
if self.index and isinstance(self.df.index, MultiIndex):
coloffset = len(self.df.index[0]) - 1

if self.merge_cells:
Expand Down Expand Up @@ -1412,10 +1412,11 @@ def _format_header_regular(self):
has_aliases = isinstance(self.header, (tuple, list, np.ndarray))
if has_aliases or self.header:
coloffset = 0

if self.index:
coloffset = 1
if isinstance(self.df.index, MultiIndex):
coloffset = len(self.df.index[0])
if isinstance(self.df.index, MultiIndex):
coloffset = len(self.df.index[0])

colnames = self.columns
if has_aliases:
Expand Down
27 changes: 26 additions & 1 deletion pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ def test_roundtrip_indexlabels(self):
has_index_names=self.merge_cells
).astype(np.int64)
frame.index.names = ['test']
tm.assert_frame_equal(frame,recons.astype(bool))
tm.assert_frame_equal(frame, recons.astype(bool))

with ensure_clean(self.ext) as path:

Expand Down Expand Up @@ -715,6 +715,31 @@ def test_to_excel_multiindex_dates(self):
tm.assert_frame_equal(tsframe, recons)
self.assertEquals(recons.index.names, ('time', 'foo'))

def test_to_excel_multiindex_no_write_index(self):
_skip_if_no_xlrd()

# Test writing and re-reading a MI witout the index. GH 5616.

# Initial non-MI frame.
frame1 = pd.DataFrame({'a': [10, 20], 'b': [30, 40], 'c': [50, 60]})

# Add a MI.
frame2 = frame1.copy()
multi_index = pd.MultiIndex.from_tuples([(70, 80), (90, 100)])
frame2.index = multi_index

with ensure_clean(self.ext) as path:

# Write out to Excel without the index.
frame2.to_excel(path, 'test1', index=False)

# Read it back in.
reader = ExcelFile(path)
frame3 = reader.parse('test1')

# Test that it is the same as the initial frame.
tm.assert_frame_equal(frame1, frame3)

def test_to_excel_float_format(self):
_skip_if_no_xlrd()

Expand Down