Skip to content

Commit 00d369b

Browse files
committed
TST: Test unnamed columns with index_col for Excel
Closes pandas-devgh-18792.
1 parent 0e7cf48 commit 00d369b

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
@@ -1402,7 +1402,7 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
14021402
- Bug in :meth:`read_csv()` in which unnecessary warnings were being raised when the dialect's values conflicted with the default arguments (:issue:`23761`)
14031403
- Bug in :meth:`read_html()` in which the error message was not displaying the valid flavors when an invalid one was provided (:issue:`23549`)
14041404
- Bug in :meth:`read_excel()` in which extraneous header names were extracted, even though none were specified (:issue:`11733`)
1405-
- Bug in :meth:`read_excel()` in which ``index_col=None`` was not being respected and parsing index columns anyway (:issue:`20480`)
1405+
- Bug in :meth:`read_excel()` in which ``index_col=None`` was not being respected and parsing index columns anyway (:issue:`18792`, :issue:`20480`)
14061406
- Bug in :meth:`read_excel()` in which ``usecols`` was not being validated for proper column names when passed in as a string (:issue:`20480`)
14071407
- :func:`DataFrame.to_string()`, :func:`DataFrame.to_html()`, :func:`DataFrame.to_latex()` will correctly format output when a string is passed as the ``float_format`` argument (:issue:`21625`, :issue:`22270`)
14081408

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
@@ -11,7 +11,7 @@
1111
from numpy import nan
1212
import pytest
1313

14-
from pandas.compat import PY36, BytesIO, iteritems, map, range, u
14+
from pandas.compat import PY2, PY36, BytesIO, iteritems, map, range, u
1515
import pandas.util._test_decorators as td
1616

1717
import pandas as pd
@@ -264,6 +264,23 @@ def test_index_col_empty(self, ext):
264264
names=["A", "B", "C"]))
265265
tm.assert_frame_equal(result, expected)
266266

267+
@pytest.mark.parametrize("index_col", [None, 2])
268+
def test_index_col_with_unnamed(self, ext, index_col):
269+
# see gh-18792
270+
result = self.get_exceldf("test1", ext, "Sheet4",
271+
index_col=index_col)
272+
expected = DataFrame([["i1", "a", "x"], ["i2", "b", "y"]],
273+
columns=["Unnamed: 0", "col1", "col2"])
274+
if index_col:
275+
expected = expected.set_index(expected.columns[index_col])
276+
277+
# Python 2.x somehow interprets column
278+
# type as "mixed" instead of a string-type.
279+
280+
check_column_type = not PY2
281+
tm.assert_frame_equal(result, expected,
282+
check_column_type=check_column_type)
283+
267284
def test_usecols_pass_non_existent_column(self, ext):
268285
msg = ("Usecols do not match columns, "
269286
"columns expected but not found: " + r"\['E'\]")

0 commit comments

Comments
 (0)