Skip to content

Commit f622d45

Browse files
committed
TST: Test unnamed columns with index_col for Excel
Closes gh-18792.
1 parent 94ce05d commit f622d45

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

doc/source/whatsnew/v0.24.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
13961396
- Bug in :meth:`read_csv()` in which unnecessary warnings were being raised when the dialect's values conflicted with the default arguments (:issue:`23761`)
13971397
- Bug in :meth:`read_html()` in which the error message was not displaying the valid flavors when an invalid one was provided (:issue:`23549`)
13981398
- Bug in :meth:`read_excel()` in which extraneous header names were extracted, even though none were specified (:issue:`11733`)
1399-
- Bug in :meth:`read_excel()` in which ``index_col=None`` was not being respected and parsing index columns anyway (:issue:`20480`)
1399+
- Bug in :meth:`read_excel()` in which ``index_col=None`` was not being respected and parsing index columns anyway (:issue:`18792`, :issue:`20480`)
14001400
- Bug in :meth:`read_excel()` in which ``usecols`` was not being validated for proper column names when passed in as a string (:issue:`20480`)
14011401

14021402
Plotting

pandas/tests/io/data/test1.xls

512 Bytes
Binary file not shown.

pandas/tests/io/data/test1.xlsm

895 Bytes
Binary file not shown.

pandas/tests/io/data/test1.xlsx

896 Bytes
Binary file not shown.

pandas/tests/io/test_excel.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pandas.util.testing as tm
1616
import pandas.util._test_decorators as td
1717
from pandas import DataFrame, Index, MultiIndex, Series
18-
from pandas.compat import u, range, map, BytesIO, iteritems, PY36
18+
from pandas.compat import u, range, map, BytesIO, iteritems, PY2, PY36
1919
from pandas.core.config import set_option, get_option
2020
from pandas.io.common import URLError
2121
from pandas.io.excel import (
@@ -245,6 +245,23 @@ def test_index_col_empty(self, ext):
245245
names=["A", "B", "C"]))
246246
tm.assert_frame_equal(result, expected)
247247

248+
@pytest.mark.parametrize("index_col", [None, 2])
249+
def test_index_col_with_unnamed(self, ext, index_col):
250+
# see gh-18792
251+
result = self.get_exceldf("test1", ext, "Sheet4",
252+
index_col=index_col)
253+
expected = DataFrame([["i1", "a", "x"], ["i2", "b", "y"]],
254+
columns=["Unnamed: 0", "col1", "col2"])
255+
256+
if index_col:
257+
expected = expected.set_index(expected.columns[index_col])
258+
259+
# Python 2.x somehow interprets column
260+
# type as "mixed" instead of a string-type.
261+
check_column_type = not PY2
262+
tm.assert_frame_equal(result, expected,
263+
check_column_type=check_column_type)
264+
248265
def test_usecols_pass_non_existent_column(self, ext):
249266
msg = ("Usecols do not match columns, "
250267
"columns expected but not found: " + r"\['E'\]")

0 commit comments

Comments
 (0)