BUG: DataFrame.to_excel() now raises if column parameter contains duplicates · Issue #39695 · pandas-dev/pandas (original) (raw)


Code Sample, a copy-pastable example

import pandas as pd dft = pd.DataFrame({"A": [0, 1], "B": [10, 11]}) dft.to_excel(r"c:\test\test3.xlsx", columns=["A", "B", "A"])

Problem description

The example works with pandas 1.1.0, but not with pandas 1.2.1 any more. With 1.2.1 it raises:

Traceback (most recent call last): File "", line 1, in File "c:\test\venv4\lib\site-packages\pandas\core\generic.py", line 2177, in to_excel formatter = ExcelFormatter( File "c:\test\venv4\lib\site-packages\pandas\io\formats\excel.py", line 470, in init raise KeyError("Not all names specified in 'columns' are found") KeyError: "Not all names specified in 'columns' are found"

If the column argument doesn't contain duplicates (e.g. columns=["A", "B"]) it works also in 1.2.1

In the documentation of .to_excel() I found no information about an intended change in behaviour.

Expected Output

I expected it to work like with pandas 1.1.0, producing an excel file with the following content:

image

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 9d598a5
python : 3.9.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : de_DE.cp1252

pandas : 1.2.1
numpy : 1.20.0
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 53.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None