Skip to content

Commit 418bc65

Browse files
committed
BUG: Convert data to string for Python 2.x
1 parent 00d369b commit 418bc65

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

doc/source/whatsnew/v0.24.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,8 @@ 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:`18792`, :issue:`20480`)
1405+
- Bug in :meth:`read_excel()` in which ``index_col=None`` was not being respected and parsing index columns anyway (:issue:`20480`)
1406+
- Bug in :meth:`read_excel()` in which data columns were not being converted to string with Python 2.x (:issue:`23874`)
14061407
- Bug in :meth:`read_excel()` in which ``usecols`` was not being validated for proper column names when passed in as a string (:issue:`20480`)
14071408
- :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`)
14081409

pandas/io/excel.py

+7
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ def _parse_cell(cell_contents, cell_typ):
554554
val = int(cell_contents)
555555
if val == cell_contents:
556556
cell_contents = val
557+
557558
return cell_contents
558559

559560
ret_dict = False
@@ -838,6 +839,12 @@ def _fill_mi_header(row, control_row):
838839
control_row[i] = False
839840
last = row[i]
840841

842+
if compat.PY2:
843+
try:
844+
row[i] = str(row[i])
845+
except UnicodeEncodeError:
846+
pass
847+
841848
return row, control_row
842849

843850
# fill blank if index_col not None

pandas/tests/io/test_excel.py

+8-13
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 PY2, PY36, BytesIO, iteritems, map, range, u
14+
from pandas.compat import PY36, BytesIO, iteritems, map, range, u
1515
import pandas.util._test_decorators as td
1616

1717
import pandas as pd
@@ -274,12 +274,7 @@ def test_index_col_with_unnamed(self, ext, index_col):
274274
if index_col:
275275
expected = expected.set_index(expected.columns[index_col])
276276

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)
277+
tm.assert_frame_equal(result, expected)
283278

284279
def test_usecols_pass_non_existent_column(self, ext):
285280
msg = ("Usecols do not match columns, "
@@ -940,9 +935,9 @@ def test_read_excel_multiindex_empty_level(self, ext):
940935
})
941936

942937
expected = DataFrame({
943-
("One", u"x"): {0: 1},
944-
("Two", u"X"): {0: 3},
945-
("Two", u"Y"): {0: 7},
938+
("One", "x"): {0: 1},
939+
("Two", "X"): {0: 3},
940+
("Two", "Y"): {0: 7},
946941
("Zero", "Unnamed: 4_level_1"): {0: 0}
947942
})
948943

@@ -959,9 +954,9 @@ def test_read_excel_multiindex_empty_level(self, ext):
959954

960955
expected = pd.DataFrame({
961956
("Beg", "Unnamed: 1_level_1"): {0: 0},
962-
("Middle", u"x"): {0: 1},
963-
("Tail", u"X"): {0: 3},
964-
("Tail", u"Y"): {0: 7}
957+
("Middle", "x"): {0: 1},
958+
("Tail", "X"): {0: 3},
959+
("Tail", "Y"): {0: 7}
965960
})
966961

967962
df.to_excel(path)

0 commit comments

Comments
 (0)