Skip to content

Commit c202599

Browse files
committed
Add round trip test
Added round trip test and fixed error in writing sheets when merge_cells=false and columns have multi index
1 parent 8f9e523 commit c202599

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

pandas/core/format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,8 +1867,8 @@ def _format_hierarchical_rows(self):
18671867

18681868
# MultiIndex columns require an extra row
18691869
# with index names (blank if None) for
1870-
# unambigous round-trip
1871-
if isinstance(self.columns, MultiIndex):
1870+
# unambigous round-trip, but only if merged
1871+
if isinstance(self.columns, MultiIndex) and not self.merge_cells:
18721872
self.rowcounter += 1
18731873

18741874
# if index labels are not empty go ahead and dump

pandas/io/parsers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -866,19 +866,19 @@ def _extract_multi_indexer_columns(self, header, index_names, col_names,
866866
# extract the columns
867867
field_count = len(header[0])
868868

869-
def tostr(x):
870-
return str(x) if not isinstance(x, compat.string_types) else x
871-
872869
def extract(r):
873-
return tuple([tostr(r[i]) for i in range(field_count) if i not in sic])
870+
return tuple([r[i] for i in range(field_count) if i not in sic])
874871

875872
columns = lzip(*[extract(r) for r in header])
876873
names = ic + columns
877874

875+
def tostr(x):
876+
return str(x) if not isinstance(x, compat.string_types) else x
877+
878878
# if we find 'Unnamed' all of a single level, then our header was too
879879
# long
880880
for n in range(len(columns[0])):
881-
if all(['Unnamed' in c[n] for c in columns]):
881+
if all(['Unnamed' in tostr(c[n]) for c in columns]):
882882
raise _parser.CParserError(
883883
"Passed header=[%s] are too many rows for this "
884884
"multi_index of columns"

pandas/io/tests/test_excel.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ def test_read_excel_multiindex(self):
558558
tm.assert_frame_equal(actual, expected)
559559

560560
# Issue #11317
561-
expected.columns = mi.set_levels(['1','2'],level=1).set_names(['c1', 'c2'])
561+
expected.columns = mi.set_levels([1,2],level=1).set_names(['c1', 'c2'])
562562
actual = read_excel(mi_file, 'name_with_int', index_col=0, header=[0,1])
563563
tm.assert_frame_equal(actual, expected)
564564

@@ -1074,6 +1074,31 @@ def test_to_excel_multiindex(self):
10741074
parse_dates=False)
10751075
tm.assert_frame_equal(frame, df)
10761076
self.assertEqual(frame.index.names, df.index.names)
1077+
1078+
def test_to_excel_multiindex_cols(self):
1079+
_skip_if_no_xlrd()
1080+
if not self.merge_cells:
1081+
raise nose.SkipTest('Skip tests MI on cols with no merging.')
1082+
1083+
1084+
frame = self.frame
1085+
arrays = np.arange(len(frame.index) * 2).reshape(2, -1)
1086+
new_index = MultiIndex.from_arrays(arrays,
1087+
names=['first', 'second'])
1088+
frame.index = new_index
1089+
1090+
new_cols_index = MultiIndex.from_tuples([(40,1),(40,2),(50,1),(50,2)])
1091+
frame.columns = new_cols_index
1092+
1093+
with ensure_clean(self.ext) as path:
1094+
# round trip
1095+
frame.to_excel(path, 'test1', merge_cells=self.merge_cells)
1096+
reader = ExcelFile(path)
1097+
df = read_excel(reader, 'test1', header=[0,1],
1098+
index_col=[0, 1],
1099+
parse_dates=False)
1100+
tm.assert_frame_equal(frame, df)
1101+
self.assertEqual(frame.index.names, df.index.names)
10771102

10781103
def test_to_excel_multiindex_dates(self):
10791104
_skip_if_no_xlrd()

0 commit comments

Comments
 (0)