Skip to content

Commit

Permalink
Backport PR pandas-dev#39800: Regression in to_excel when setting dup…
Browse files Browse the repository at this point in the history
…licate column names
  • Loading branch information
phofl authored and meeseeksmachine committed Feb 15, 2021
1 parent e96bb37 commit c7741e1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.3.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 :func:`pandas.to_excel` raising ``KeyError`` when giving duplicate columns with ``columns`` attribute (:issue:`39695`)
-

.. ---------------------------------------------------------------------------
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 @@ -465,7 +465,7 @@ def __init__(
if not len(Index(cols).intersection(df.columns)):
raise KeyError("passes columns are not ALL present dataframe")

if len(Index(cols).intersection(df.columns)) != len(cols):
if len(Index(cols).intersection(df.columns)) != len(set(cols)):
# Deprecated in GH#17295, enforced in 1.0.0
raise KeyError("Not all names specified in 'columns' are found")

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,15 @@ def test_raise_when_saving_timezones(self, dtype, tz_aware_fixture, path):
with pytest.raises(ValueError, match="Excel does not support"):
df.to_excel(path)

def test_excel_duplicate_columns_with_names(self, path):
# GH#39695
df = DataFrame({"A": [0, 1], "B": [10, 11]})
df.to_excel(path, columns=["A", "B", "A"], index=False)

result = pd.read_excel(path)
expected = DataFrame([[0, 10, 0], [1, 11, 1]], columns=["A", "B", "A.1"])
tm.assert_frame_equal(result, expected)


class TestExcelWriterEngineTests:
@pytest.mark.parametrize(
Expand Down

0 comments on commit c7741e1

Please sign in to comment.