Skip to content

BUG: read_excel: incorrect multi-index values - 1 (empty or equal values) #33476

Closed
@kuraga

Description

@kuraga
  • 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

pd.read_excel('df.xlsx', sheet_name='Sheet1', index_col=0, header=[0, 1])

Note: engines could be different.

Problem description

Exception on pd.read_excel():

$ ~/miniconda3/bin/python read.py 
Traceback (most recent call last):
  File "read.py", line 4, in <module>
    df = pd.read_excel('df_empty.xlsx', engine='openpyxl', sheet_name='Sheet1', index_col=0, header=[0, 1])
  File "/home/sasha/miniconda3/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 334, in read_excel
    **kwds,
  File "/home/sasha/miniconda3/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 888, in parse
    **kwds,
  File "/home/sasha/miniconda3/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 521, in parse
    ].columns.set_names(header_names)
  File "/home/sasha/miniconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 1325, in set_names
    idx._set_names(names, level=level)
  File "/home/sasha/miniconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 1239, in _set_names
    raise ValueError(f"Length of new names must be 1, got {len(values)}")
ValueError: Length of new names must be 1, got 2

on XLSX like:
image
(C2 is empty, df_empty.xlsx)

or this:

image
(B2 == C2, df_equals.xlsx)

Expected Output

No exceptions.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.7.7.final.0 python-bits : 64 OS : Linux OS-release : 5.4.25-calculate machine : x86_64 processor : Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz byteorder : little LC_ALL : None LANG : ru_RU.utf8 LOCALE : ru_RU.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : 0.29.15
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
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 : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : 1.2.8
numba : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions