Skip to content

BUG: pandas.read_excel creates a DataFrame with incorrect multi-level columns #34188

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

Open
2 of 3 tasks
LastsForever opened this issue May 15, 2020 · 3 comments
Open
2 of 3 tasks
Labels
Docs IO Excel read_excel, to_excel

Comments

@LastsForever
Copy link

LastsForever commented May 15, 2020

  • 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

There is a file a.xlsx with only index and multi-level columns:
image
pd.read_excel('a.xlsx', index_col=0, header=[0, 1]) creates a DataFrame with incorrect multi-level columns:
image

However, if the file b.xlsx with the same structure (shown below) is not empty, the result appears to be correct:
C559F1A1-36E2-4B1E-B6EF-8AD1EDAE7420
image

Problem description

The parameter header=[0, 1] indicates that the first two rows of the excel file should be used as multi-level columns, but the result got first three rows as the multi-level columns.

Expected Output

pd.read_excel('a.xlsx', index_col=0, header=[0, 1]) should get the DataFrame:
image

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.2.final.0
python-bits : 64
OS : Darwin
OS-release : 19.4.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.2.0.post20200511
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : None
numba : None

@LastsForever LastsForever added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 15, 2020
@LastsForever LastsForever changed the title BUG: pandas.read_excel creating a DataFrame with incorrect multi columns BUG: pandas.read_excel creates a DataFrame with incorrect multi columns May 15, 2020
@LastsForever LastsForever changed the title BUG: pandas.read_excel creates a DataFrame with incorrect multi columns BUG: pandas.read_excel creates a DataFrame with incorrect multi-level columns May 15, 2020
@charlesdong1991 charlesdong1991 added IO Excel read_excel, to_excel and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 16, 2020
@rhshadrach
Copy link
Member

Thanks for the report! It seems writing might have a related issue. Doing:

df = pd.DataFrame({'a': 3 * [None]}, index=[1, 2, 3])
df.columns = pd.MultiIndex.from_arrays([['A'], ['x']])
df.to_excel('test.xlsx')

produces an empty line below the columns. Reading this in produces the original DataFrame correctly. If I first delete this empty line and then read in the excel file, I get the issue mentioned above.

@rhshadrach rhshadrach added this to the Contributions Welcome milestone Mar 15, 2021
@ahawryluk
Copy link
Contributor

It looks like this behaviour was intentional. The reader interprets the blank spaces the same way the writer does, and "in the case of column MultiIndex a blank row ALWAYS has to be inserted so the format is unambiguous. I believe this is unavoidable (csv does it too) - but the output looks a little odd if the index doesn't have names." #10967 (comment)

Also, #10967 (comment) points out this exact case as being ambiguous:

spreadsheet screenshot

There's no way to know if "a" is the index name or if the data row is blank. Pandas assumes that "a" is the index name because that's how it writes named MulitIndex dataframes.

@rhshadrach
Copy link
Member

Thanks @ahawryluk. I don't see any options that would improve upon this, but perhaps a note in the documentation of read_excel may be useful.

@mroeschke mroeschke added Docs and removed Bug labels Aug 7, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs IO Excel read_excel, to_excel
Projects
None yet
Development

No branches or pull requests

5 participants