diff --git a/pandas/core/format.py b/pandas/core/format.py index 5062fd9be6357..ae0d95b1c3074 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -1393,8 +1393,12 @@ def _format_regular_rows(self): for idx, idxval in enumerate(index_values): yield ExcelCell(self.rowcounter + idx, 0, idxval, header_style) + # Get a frame that will account for any duplicates in the column names. + col_mapped_frame = self.df.loc[:, self.columns] + + # Write the body of the frame data series by series. for colidx in range(len(self.columns)): - series = self.df.iloc[:, colidx] + series = col_mapped_frame.iloc[:, colidx] for i, val in enumerate(series): yield ExcelCell(self.rowcounter + i, colidx + coloffset, val) @@ -1461,8 +1465,12 @@ def _format_hierarchical_rows(self): header_style) gcolidx += 1 + # Get a frame that will account for any duplicates in the column names. + col_mapped_frame = self.df.loc[:, self.columns] + + # Write the body of the frame data series by series. for colidx in range(len(self.columns)): - series = self.df.iloc[:, colidx] + series = col_mapped_frame.iloc[:, colidx] for i, val in enumerate(series): yield ExcelCell(self.rowcounter + i, gcolidx + colidx, val) diff --git a/pandas/io/tests/test_excel.py b/pandas/io/tests/test_excel.py index 8bcf5e461ce7c..a126bd965e4b6 100644 --- a/pandas/io/tests/test_excel.py +++ b/pandas/io/tests/test_excel.py @@ -922,11 +922,25 @@ def test_duplicated_columns(self): write_frame.columns = colnames write_frame.to_excel(path, 'test1') - read_frame = read_excel(path, 'test1').astype(np.int64) + read_frame = read_excel(path, 'test1') read_frame.columns = colnames tm.assert_frame_equal(write_frame, read_frame) + def test_swapped_columns(self): + # Test for issue #5427. + _skip_if_no_xlrd() + + with ensure_clean(self.ext) as path: + write_frame = DataFrame({'A': [1, 1, 1], + 'B': [2, 2, 2]}) + write_frame.to_excel(path, 'test1', cols=['B', 'A']) + + read_frame = read_excel(path, 'test1', header=0) + + tm.assert_series_equal(write_frame['A'], read_frame['A']) + tm.assert_series_equal(write_frame['B'], read_frame['B']) + class OpenpyxlTests(ExcelWriterBase, unittest.TestCase): ext = '.xlsx'