Skip to content

Commit

Permalink
Backport PR pandas-dev#31729: BUG: Fix to_excel writers handling of cols
Browse files Browse the repository at this point in the history
  • Loading branch information
alimcmaster1 authored and meeseeksmachine committed Feb 6, 2020
1 parent 984f18d commit 041229e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~

-
- Fixed regression in :meth:`DataFrame.to_excel` when ``columns`` kwarg is passed (:issue:`31677`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,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 @@ -1018,6 +1018,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

0 comments on commit 041229e

Please sign in to comment.