Skip to content

BUG: read_excel forward-filling MI names #38517

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

Merged
merged 10 commits into from
Jan 1, 2021
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ I/O
- Bug in :func:`read_csv` not accepting ``usecols`` with different length than ``names`` for ``engine="python"`` (:issue:`16469`)
- Bug in :func:`read_csv` raising ``TypeError`` when ``names`` and ``parse_dates`` is specified for ``engine="c"`` (:issue:`33699`)
- Allow custom error values for parse_dates argument of :func:`read_sql`, :func:`read_sql_query` and :func:`read_sql_table` (:issue:`35185`)
- Bug in :func:`read_excel` forward filling :class:`MultiIndex` names with multiple header and index columns specified (:issue:`34673`)
-

Period
Expand Down
9 changes: 7 additions & 2 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ def parse(
header_name, _ = pop_header_name(data[row], index_col)
header_names.append(header_name)

has_index_names = is_list_like(header) and len(header) > 1

if is_list_like(index_col):
# Forward fill values for MultiIndex index.
if header is None:
Expand All @@ -512,6 +514,11 @@ def parse(
else:
offset = 1 + max(header)

# GH34673: if MultiIndex names present and not defined in the header,
# don't forward fill the names
if has_index_names:
offset += 1

# Check if we have an empty dataset
# before trying to collect data.
if offset < len(data):
Expand All @@ -524,8 +531,6 @@ def parse(
else:
last = data[row][col]

has_index_names = is_list_like(header) and len(header) > 1

# GH 12292 : error when read one empty column from excel file
try:
parser = TextParser(
Expand Down
Binary file modified pandas/tests/io/data/excel/testmultiindex.ods
Binary file not shown.
Binary file modified pandas/tests/io/data/excel/testmultiindex.xls
Binary file not shown.
Binary file modified pandas/tests/io/data/excel/testmultiindex.xlsb
Binary file not shown.
Binary file modified pandas/tests/io/data/excel/testmultiindex.xlsm
Binary file not shown.
Binary file modified pandas/tests/io/data/excel/testmultiindex.xlsx
Binary file not shown.
13 changes: 13 additions & 0 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,19 @@ def test_read_excel_multiindex(self, read_ext):
)
tm.assert_frame_equal(actual, expected)

# GH34673: "both_name_blank_after_mi_name" sheet
expected.index = MultiIndex.from_tuples(
[("foo", np.nan), ("foo", "b"), ("bar", "a"), ("bar", "b")],
names=["ilvl1", "ilvl2"],
)
actual = pd.read_excel(
mi_file,
sheet_name="both_name_blank_after_mi_name",
index_col=[0, 1],
header=[0, 1],
)
tm.assert_frame_equal(actual, expected)

def test_read_excel_multiindex_header_only(self, read_ext):
# see gh-11733.
#
Expand Down