Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
asishm opened this issue Jul 5, 2024 · 2 comments · May be fixed by #59188
Open
3 tasks done

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

asishm opened this issue Jul 5, 2024 · 2 comments · May be fixed by #59188
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@asishm
Copy link
Contributor

asishm commented Jul 5, 2024

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

@asishm asishm added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 5, 2024
@asishm
Copy link
Contributor Author

asishm commented Jul 5, 2024

Looking at it a bit more, isinstance(pd.Timestamp(...), datetime) returns True and same with isinstance(pd.Timedelta(...), timedelta), so maybe it's moot.

@Riyazul555 Riyazul555 linked a pull request Jul 5, 2024 that will close this issue
4 tasks
@rhshadrach
Copy link
Member

I don't see a very strong reason to prefer one over the other. When doing inference for constructors, we store these as datetime.date objects. E.g.

import datetime

print(type(pd.Series(datetime.date(2024, 3, 10)).iloc[0]))
# <class 'datetime.date'>

In addition, openpyxl has been around much longer than calamine. Both of these suggest to me we should return the Python objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants