Skip to content

BUG/TST: Fix Excel writers with duplicated column names. #5237

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
Oct 16, 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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down