Skip to content

Commit 396737f

Browse files
committed
BUG: Fix inconsistent datetime types between calamine and openpyxl readers (#59186)
The calamine Excel reader was returning pd.Timestamp and pd.Timedelta for datetime values in mixed-type columns, while openpyxl returns standard library datetime.datetime and datetime.timedelta objects. This change modifies the calamine reader to return standard library datetime types, ensuring consistent behavior across Excel reader engines. - Return datetime.datetime as-is instead of converting to pd.Timestamp - Convert date objects to datetime to match openpyxl behavior - Return datetime.timedelta as-is instead of converting to pd.Timedelta - Remove unused pandas import - Update test helper get_exp_unit() to reflect new consistent behavior
1 parent 47fea80 commit 396737f

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

pandas/io/excel/_calamine.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from pandas.compat._optional import import_optional_dependency
1616
from pandas.util._decorators import doc
1717

18-
import pandas as pd
1918
from pandas.core.shared_docs import _shared_docs
2019

2120
from pandas.io.excel._base import BaseExcelReader
@@ -107,10 +106,16 @@ def _convert_cell(value: _CellValue) -> Scalar | NaTType | time:
107106
return val
108107
else:
109108
return value
109+
elif isinstance(value, datetime):
110+
# Return datetime.datetime as-is to match openpyxl behavior (GH#59186)
111+
return value
110112
elif isinstance(value, date):
111-
return pd.Timestamp(value)
113+
# Convert date to datetime to match openpyxl behavior (GH#59186)
114+
return datetime(value.year, value.month, value.day)
112115
elif isinstance(value, timedelta):
113-
return pd.Timedelta(value)
116+
# Return datetime.timedelta as-is to match openpyxl behavior (GH#59186)
117+
# (previously returned pd.Timedelta)
118+
return value
114119
elif isinstance(value, time):
115120
return value
116121

pandas/tests/io/excel/test_readers.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,7 @@ def df_ref(datapath):
135135

136136

137137
def get_exp_unit(read_ext: str, engine: str | None) -> str:
138-
unit = "us"
139-
if read_ext == ".ods" and engine == "odf":
140-
pass
141-
elif (read_ext == ".ods") ^ (engine == "calamine"):
142-
unit = "s"
143-
return unit
138+
return "us"
144139

145140

146141
def adjust_expected(expected: DataFrame, read_ext: str, engine: str | None) -> None:

0 commit comments

Comments
 (0)