Skip to content

BUG: Difference between calamine and openpyxl readers - columns with mixed data types #59186

Open
@asishm

Description

@asishm

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
from io import BytesIO
from datetime import datetime

df = pd.DataFrame({'a': ['abc', np.nan, datetime.now(), 'def']})

out = BytesIO()
df.to_excel(out, index=False)
df_openpyxl = pd.read_excel(out, engine='openpyxl')
df_calamine = pd.read_excel(out, engine='calamine')

In [49]: df_openpyxl['a'].map(type)
Out[49]:
0                  <class 'str'>
1                <class 'float'>
2    <class 'datetime.datetime'>
3                  <class 'str'>
Name: a, dtype: object

In [50]: df_calamine['a'].map(type)
Out[50]:
0                                        <class 'str'>
1                                      <class 'float'>
2    <class 'pandas._libs.tslibs.timestamps.Timesta...
3                                        <class 'str'>
Name: a, dtype: object

Issue Description

When trying to read an excel file that has mixed formats, using the calamine engine - results in an object dtype column where the value is pandas._libs.tslibs.timestamps.Timestamp and pandas._libs.tslibs.timedeltas.Timedelta whereas with the openpyxl engine, these values are datetime.datetime and datetime.timedelta instead.

The difference seems to be because the calamine implementation explicitly sets pd.Timestamp/pd.Timedelta data types

elif isinstance(value, date):
return pd.Timestamp(value)
elif isinstance(value, timedelta):
return pd.Timedelta(value)
elif isinstance(value, time):
# cast needed here because Scalar doesn't include datetime.time
return cast(Scalar, value)

whereas the openpyxl implementation uses whatever the library returns.

Expected Behavior

The individual item data types should match

Installed Versions

INSTALLED VERSIONS

commit : dd87dd3
python : 3.10.14
python-bits : 64
OS : Linux
OS-release : 4.19.128-microsoft-standard
Version : #1 SMP Tue Jun 23 12:58:10 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 3.0.0.dev0+1113.gdd87dd3ef6
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0
pip : 24.0
Cython : 3.0.10
sphinx : 7.3.7
IPython : 8.25.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : 1.3.8
fastparquet : 2024.5.0
fsspec : 2024.6.0
html5lib : 1.1
hypothesis : 6.103.0
gcsfs : 2024.6.0
jinja2 : 3.1.4
lxml.etree : 5.2.2
matplotlib : 3.8.4
numba : 0.59.1
numexpr : 2.10.0
odfpy : None
openpyxl : 3.1.2
psycopg2 : 2.9.9
pymysql : 1.4.6
pyarrow : 16.1.0
pyreadstat : 1.2.7
pytest : 8.2.1
python-calamine : None
pyxlsb : 1.0.10
s3fs : 2024.6.0
scipy : 1.13.1
sqlalchemy : 2.0.30
tables : 3.9.2
tabulate : 0.9.0
xarray : 2024.5.0
xlrd : 2.0.1
xlsxwriter : 3.1.9
zstandard : 0.19.0
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO Excelread_excel, to_excelNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions