Skip to content
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

BUG: Fix to_excel writers handling of cols #31729

Merged
merged 4 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 1 deletion pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def __init__(
# Deprecated in GH#17295, enforced in 1.0.0
raise KeyError("Not all names specified in 'columns' are found")

self.df = df
self.df = df.reindex(columns=cols)

self.columns = self.df.columns
self.float_format = float_format
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,27 @@ def test_invalid_columns(self, path):
):
write_frame.to_excel(path, "test1", columns=["C", "D"])

@pytest.mark.parametrize(
"to_excel_index,read_excel_index_col",
[
(True, 0), # Include index in write to file
(False, None), # Dont include index in write to file
],
)
def test_write_subset_columns(self, path, to_excel_index, read_excel_index_col):
# GH 31677
write_frame = DataFrame({"A": [1, 1, 1], "B": [2, 2, 2], "C": [3, 3, 3]})
write_frame.to_excel(
path, "col_subset_bug", columns=["A", "B"], index=to_excel_index
)

expected = write_frame[["A", "B"]]
read_frame = pd.read_excel(
path, "col_subset_bug", index_col=read_excel_index_col
)

tm.assert_frame_equal(expected, read_frame)

def test_comment_arg(self, path):
# see gh-18735
#
Expand Down