From 49a32d859790a4e6ca642e6307bc40e9add4b686 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Thu, 6 Feb 2020 00:23:47 +0000 Subject: [PATCH 1/3] Bugfix to_excel writers --- pandas/io/formats/excel.py | 2 +- pandas/tests/io/excel/test_writers.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 14e79538541af..28a069bc9fc1b 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -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 diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index f7b49ccb1a72d..1283a20315b43 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -1048,6 +1048,22 @@ 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 # From f68798ef2be49fd2d5074900fdcd74d17c76645a Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Thu, 6 Feb 2020 00:32:27 +0000 Subject: [PATCH 2/3] Run black --- pandas/tests/io/excel/test_writers.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index 1283a20315b43..91665a24fc4c5 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -1051,16 +1051,21 @@ def test_invalid_columns(self, path): @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 - ]) + (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) + 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) + read_frame = pd.read_excel( + path, "col_subset_bug", index_col=read_excel_index_col + ) tm.assert_frame_equal(expected, read_frame) From 91b22e26d1ab4d4c1667c9d50db82ddcde9f4da7 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Thu, 6 Feb 2020 21:26:30 +0000 Subject: [PATCH 3/3] Add whatsnew --- doc/source/whatsnew/v1.0.2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.2.rst b/doc/source/whatsnew/v1.0.2.rst index 07a837829c384..94dc1e0c007ca 100644 --- a/doc/source/whatsnew/v1.0.2.rst +++ b/doc/source/whatsnew/v1.0.2.rst @@ -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`) - .. ---------------------------------------------------------------------------