Skip to content

Commit c15f080

Browse files
Dr-IrvWillAyd
andauthored
Fix Issue 34748 - read in datetime as MultiIndex for column headers (pandas-dev#34954)
* Fix Issue 34748 - read in datetime as MultiIndex for column headers * add more xls file formats * use testing pattern for other Excel files * added ods file * remove xfail for ods test * skip tests on xlsb with datetimes Co-authored-by: Will Ayd <[email protected]>
1 parent ebca2a6 commit c15f080

File tree

8 files changed

+21
-1
lines changed

8 files changed

+21
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,7 @@ I/O
10511051
- Bug in :meth:`~HDFStore.create_table` now raises an error when `column` argument was not specified in `data_columns` on input (:issue:`28156`)
10521052
- :meth:`read_json` now could read line-delimited json file from a file url while `lines` and `chunksize` are set.
10531053
- Bug in :meth:`DataFrame.to_sql` when reading DataFrames with ``-np.inf`` entries with MySQL now has a more explicit ``ValueError`` (:issue:`34431`)
1054+
- Bug in "meth"`read_excel` where datetime values are used in the header in a `MultiIndex` (:issue:`34748`)
10541055

10551056
Plotting
10561057
^^^^^^^^

pandas/io/parsers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ def extract(r):
16141614
# Clean the column names (if we have an index_col).
16151615
if len(ic):
16161616
col_names = [
1617-
r[0] if (len(r[0]) and r[0] not in self.unnamed_cols) else None
1617+
r[0] if ((r[0] is not None) and r[0] not in self.unnamed_cols) else None
16181618
for r in header
16191619
]
16201620
else:
Binary file not shown.
Binary file not shown.
7.76 KB
Binary file not shown.
Binary file not shown.
8.48 KB
Binary file not shown.

pandas/tests/io/excel/test_readers.py

+19
Original file line numberDiff line numberDiff line change
@@ -1143,3 +1143,22 @@ def test_header_with_index_col(self, engine, filename):
11431143
filename, sheet_name="Sheet1", index_col=0, header=[0, 1]
11441144
)
11451145
tm.assert_frame_equal(expected, result)
1146+
1147+
def test_read_datetime_multiindex(self, engine, read_ext):
1148+
# GH 34748
1149+
if engine == "pyxlsb":
1150+
pytest.xfail("Sheets containing datetimes not supported by pyxlsb")
1151+
1152+
f = "test_datetime_mi" + read_ext
1153+
with pd.ExcelFile(f) as excel:
1154+
actual = pd.read_excel(excel, header=[0, 1], index_col=0, engine=engine)
1155+
expected_column_index = pd.MultiIndex.from_tuples(
1156+
[(pd.to_datetime("02/29/2020"), pd.to_datetime("03/01/2020"))],
1157+
names=[
1158+
pd.to_datetime("02/29/2020").to_pydatetime(),
1159+
pd.to_datetime("03/01/2020").to_pydatetime(),
1160+
],
1161+
)
1162+
expected = pd.DataFrame([], columns=expected_column_index)
1163+
1164+
tm.assert_frame_equal(expected, actual)

0 commit comments

Comments
 (0)