diff --git a/doc/source/release.rst b/doc/source/release.rst index 805b8d24d70d9..3ab1f9e05a5e5 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -611,6 +611,8 @@ Bug Fixes the original ordering (:issue:`4621`). - Fixed ``Period`` with a business date freq to always roll-forward if on a non-business date. (:issue:`5203`) + - Fixed bug in Excel writers where frames with duplicate column names weren't + written correctly. (:issue: `5235`) pandas 0.12.0 ------------- diff --git a/pandas/core/format.py b/pandas/core/format.py index 4f2d9f214ce6e..2355ae16874ce 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -1371,8 +1371,8 @@ def _format_regular_rows(self): for idx, idxval in enumerate(index_values): yield ExcelCell(self.rowcounter + idx, 0, idxval, header_style) - for colidx, colname in enumerate(self.columns): - series = self.df[colname] + for colidx in range(len(self.columns)): + series = self.df.iloc[:, colidx] for i, val in enumerate(series): yield ExcelCell(self.rowcounter + i, colidx + coloffset, val) @@ -1408,8 +1408,8 @@ def _format_hierarchical_rows(self): indexcolval, header_style) gcolidx += 1 - for colidx, colname in enumerate(self.columns): - series = self.df[colname] + for colidx in range(len(self.columns)): + series = self.df.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 b279c7ffd2892..15130c552c8a8 100644 --- a/pandas/io/tests/test_excel.py +++ b/pandas/io/tests/test_excel.py @@ -874,6 +874,24 @@ def roundtrip(df, header=True, parser_hdr=0): self.assertEqual(res.shape, (1, 2)) self.assertTrue(res.ix[0, 0] is not np.nan) + def test_duplicated_columns(self): + # Test for issue #5235. + _skip_if_no_xlrd() + ext = self.ext + path = '__tmp_to_excel_duplicated_columns__.' + ext + + with ensure_clean(path) as path: + write_frame = DataFrame([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) + colnames = ['A', 'B', 'B'] + + write_frame.columns = colnames + write_frame.to_excel(path, 'test1') + + read_frame = read_excel(path, 'test1').astype(np.int64) + read_frame.columns = colnames + + tm.assert_frame_equal(write_frame, read_frame) + class OpenpyxlTests(ExcelWriterBase, unittest.TestCase): ext = 'xlsx'