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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -267,6 +267,7 @@ I/O
- Bug in :func:`to_hdf` raising ``KeyError`` when trying to apply
for subclasses of ``DataFrame`` or ``Series`` (:issue:`33748`).
- Bug in :func:`json_normalize` resulting in the first element of a generator object not being included in the returned ``DataFrame`` (:issue:`35923`)
- Bug in :func:`read_excel` forward filling :class:`MultiIndex` names with multiple header and index columns specified (:issue:`34673`)


Period
Expand Down
10 changes: 8 additions & 2 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,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 @@ -513,6 +515,12 @@ def parse(
else:
offset = 1 + max(header)

# GH34673: if MultiIndex names present and not defined in the header,
# offset needs to be incremented so that forward filling starts
# from the first MI value instead of the name
if has_index_names:
offset += 1

# Check if we have an empty dataset
# before trying to collect data.
if offset < len(data):
Expand All @@ -525,8 +533,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.
37 changes: 37 additions & 0 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,43 @@ def test_read_excel_multiindex(self, read_ext):
)
tm.assert_frame_equal(actual, expected)

@pytest.mark.parametrize(
"sheet_name,idx_lvl2",
[
("both_name_blank_after_mi_name", [np.nan, "b", "a", "b"]),
("both_name_multiple_blanks", [np.nan] * 4),
],
)
def test_read_excel_multiindex_blank_after_name(
self, read_ext, sheet_name, idx_lvl2
):
# GH34673
if pd.read_excel.keywords["engine"] == "pyxlsb":
pytest.xfail("Sheets containing datetimes not supported by pyxlsb (GH4679")

mi_file = "testmultiindex" + read_ext
mi = MultiIndex.from_product([["foo", "bar"], ["a", "b"]], names=["c1", "c2"])
expected = DataFrame(
[
[1, 2.5, pd.Timestamp("2015-01-01"), True],
[2, 3.5, pd.Timestamp("2015-01-02"), False],
[3, 4.5, pd.Timestamp("2015-01-03"), False],
[4, 5.5, pd.Timestamp("2015-01-04"), True],
],
columns=mi,
index=MultiIndex.from_arrays(
(["foo", "foo", "bar", "bar"], idx_lvl2),
names=["ilvl1", "ilvl2"],
),
)
result = pd.read_excel(
mi_file,
sheet_name=sheet_name,
index_col=[0, 1],
header=[0, 1],
)
tm.assert_frame_equal(result, expected)

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