Skip to content

BUG: Excel writer doesn't handle "cols" option correctly #5429

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
Nov 6, 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
12 changes: 10 additions & 2 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand Down
16 changes: 15 additions & 1 deletion pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you know there's an issue with dup columns, probably good to add a test case for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtratner There is a test for duplicate columns right above this one from the previous "fix".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it.

# 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'
Expand Down