Skip to content

Commit 7153e5f

Browse files
committed
TST: Test unnamed columns with index_col for Excel
Closes pandas-devgh-18792.
1 parent ca70fe6 commit 7153e5f

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
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-6
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,18 @@ 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+
tm.assert_frame_equal(result, expected)
278+
267279
def test_usecols_pass_non_existent_column(self, ext):
268280
msg = ("Usecols do not match columns, "
269281
"columns expected but not found: " + r"\['E'\]")
@@ -923,9 +935,9 @@ def test_read_excel_multiindex_empty_level(self, ext):
923935
})
924936

925937
expected = DataFrame({
926-
("One", u"x"): {0: 1},
927-
("Two", u"X"): {0: 3},
928-
("Two", u"Y"): {0: 7},
938+
("One", "x"): {0: 1},
939+
("Two", "X"): {0: 3},
940+
("Two", "Y"): {0: 7},
929941
("Zero", "Unnamed: 4_level_1"): {0: 0}
930942
})
931943

@@ -942,9 +954,9 @@ def test_read_excel_multiindex_empty_level(self, ext):
942954

943955
expected = pd.DataFrame({
944956
("Beg", "Unnamed: 1_level_1"): {0: 0},
945-
("Middle", u"x"): {0: 1},
946-
("Tail", u"X"): {0: 3},
947-
("Tail", u"Y"): {0: 7}
957+
("Middle", "x"): {0: 1},
958+
("Tail", "X"): {0: 3},
959+
("Tail", "Y"): {0: 7}
948960
})
949961

950962
df.to_excel(path)

0 commit comments

Comments
 (0)