Skip to content

Commit

Permalink
BUG: fix read_excel error for header=None and index_col as list panda…
Browse files Browse the repository at this point in the history
  • Loading branch information
rjfs authored Jul 10, 2020
1 parent 3bcaea3 commit a9327df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ I/O
- Bug in :meth:`~HDFStore.create_table` now raises an error when `column` argument was not specified in `data_columns` on input (:issue:`28156`)
- :meth:`read_json` now could read line-delimited json file from a file url while `lines` and `chunksize` are set.
- Bug in :meth:`DataFrame.to_sql` when reading DataFrames with ``-np.inf`` entries with MySQL now has a more explicit ``ValueError`` (:issue:`34431`)
- Bug in :meth:`read_excel` that was raising a ``TypeError`` when ``header=None`` and ``index_col`` given as list (:issue:`31783`)
- Bug in "meth"`read_excel` where datetime values are used in the header in a `MultiIndex` (:issue:`34748`)

Plotting
Expand Down
4 changes: 3 additions & 1 deletion pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ def parse(

if is_list_like(index_col):
# Forward fill values for MultiIndex index.
if not is_list_like(header):
if header is None:
offset = 0
elif not is_list_like(header):
offset = 1 + header
else:
offset = 1 + max(header)
Expand Down
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 @@ -968,6 +968,19 @@ def test_deprecated_kwargs(self, read_ext):

pd.read_excel("test1" + read_ext)

def test_no_header_with_list_index_col(self, read_ext):
# GH 31783
file_name = "testmultiindex" + read_ext
data = [("B", "B"), ("key", "val"), (3, 4), (3, 4)]
idx = pd.MultiIndex.from_tuples(
[("A", "A"), ("key", "val"), (1, 2), (1, 2)], names=(0, 1)
)
expected = pd.DataFrame(data, index=idx, columns=(2, 3))
result = pd.read_excel(
file_name, sheet_name="index_col_none", index_col=[0, 1], header=None
)
tm.assert_frame_equal(expected, result)


class TestExcelFileRead:
@pytest.fixture(autouse=True)
Expand Down

0 comments on commit a9327df

Please sign in to comment.