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: DataFrame.to_excel() now raises if column parameter contains duplicates #39695

Closed
2 of 3 tasks
RagBlufThim opened this issue Feb 9, 2021 · 3 comments · Fixed by #39800
Closed
2 of 3 tasks

BUG: DataFrame.to_excel() now raises if column parameter contains duplicates #39695

RagBlufThim opened this issue Feb 9, 2021 · 3 comments · Fixed by #39800
Assignees
Labels
Bug IO Excel read_excel, to_excel Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@RagBlufThim
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


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 "<stdin>", line 1, in <module>
  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

@RagBlufThim RagBlufThim added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 9, 2021
@lithomas1
Copy link
Member

Thanks @RagBlufThim for the report! I can also reproduce this on master.
This appears to be caused by #37374. cc @jbrockmendel (https://github.com/pandas-dev/pandas/blame/879d2fb8a36cfb52e8129e60b1f808cf8937a378/pandas/io/formats/excel.py#L478)

@lithomas1 lithomas1 added IO Excel read_excel, to_excel Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 10, 2021
@lithomas1 lithomas1 added this to the 1.2.3 milestone Feb 10, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Feb 12, 2021
@simonjayhawkins
Copy link
Member

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

was also working in 1.1.4

first bad commit: [e99e5ab] BUG: Fix duplicates in intersection of multiindexes (#36927) cc @phofl

@phofl phofl self-assigned this Feb 12, 2021
@phofl
Copy link
Member

phofl commented Feb 12, 2021

Will take a look over the weekend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants