Description
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
Create an excel file nammed xl_file.xlsx
, with one worksheet named Sheet1
as following:
A | B | C |
---|---|---|
2 | 'dummy_str |
Then save it and run this code in the directory where you created the excel file :
import pandas as pd
from pathlib import Path
df = pd.read_excel(
io=Path("xl_file.xlsx"),
sheet_name="Sheet1",
dtype=object,
)
dtypes_1 = df.dtypes
dtypes_2 = df.fillna("").dtypes
>>> print(dtypes_1)
0 A object # <- expected int, or int64
1 B object
2 C object
>>> print(dtypes_2)
0 A int64 # <- when we use df.fillna(""), we have the expected behavior
1 B object
2 C object
Issue Description
The method pandas.read_excel()
with argument dtypes=object
does not prevent pandas from converting data types, as described on the offical documentation page
Expected Behavior
when we use the method pandas.read_excel()
with argument dtypes=object
, on an Excel workbook which contains int values, the dtypes of this columns should be int64
Installed Versions
pandas : 2.1.1
numpy : 1.24.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.2.1
Cython : None
pytest : 7.3.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.1.7
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : None
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None