Skip to content

Commit a9327df

Browse files
authored
BUG: fix read_excel error for header=None and index_col as list #31783 (#35035)
1 parent 3bcaea3 commit a9327df

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ I/O
10531053
- Bug in :meth:`~HDFStore.create_table` now raises an error when `column` argument was not specified in `data_columns` on input (:issue:`28156`)
10541054
- :meth:`read_json` now could read line-delimited json file from a file url while `lines` and `chunksize` are set.
10551055
- Bug in :meth:`DataFrame.to_sql` when reading DataFrames with ``-np.inf`` entries with MySQL now has a more explicit ``ValueError`` (:issue:`34431`)
1056+
- Bug in :meth:`read_excel` that was raising a ``TypeError`` when ``header=None`` and ``index_col`` given as list (:issue:`31783`)
10561057
- Bug in "meth"`read_excel` where datetime values are used in the header in a `MultiIndex` (:issue:`34748`)
10571058

10581059
Plotting

pandas/io/excel/_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ def parse(
468468

469469
if is_list_like(index_col):
470470
# Forward fill values for MultiIndex index.
471-
if not is_list_like(header):
471+
if header is None:
472+
offset = 0
473+
elif not is_list_like(header):
472474
offset = 1 + header
473475
else:
474476
offset = 1 + max(header)

pandas/tests/io/excel/test_readers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,19 @@ def test_deprecated_kwargs(self, read_ext):
968968

969969
pd.read_excel("test1" + read_ext)
970970

971+
def test_no_header_with_list_index_col(self, read_ext):
972+
# GH 31783
973+
file_name = "testmultiindex" + read_ext
974+
data = [("B", "B"), ("key", "val"), (3, 4), (3, 4)]
975+
idx = pd.MultiIndex.from_tuples(
976+
[("A", "A"), ("key", "val"), (1, 2), (1, 2)], names=(0, 1)
977+
)
978+
expected = pd.DataFrame(data, index=idx, columns=(2, 3))
979+
result = pd.read_excel(
980+
file_name, sheet_name="index_col_none", index_col=[0, 1], header=None
981+
)
982+
tm.assert_frame_equal(expected, result)
983+
971984

972985
class TestExcelFileRead:
973986
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)