-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: read_excel doesn't honor dtype for index #35816
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
Comments
Thanks for the report! We typically ask that you don't upload data and instead provide a copy-pastable code sample that people can run. See bug report guidelines: https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports |
Thanks for your reply @arw2019! I did provide a copy-pastable code sample (the second code block from the top). I also provided the contents of the Excel spreadsheet exported as CSV (topmost code block). While Excel files themselves are not "copy-pastable", I still wanted to provide the exact files I had this problem with, in case someone wants to just download them instead of copy-pasting the CSV into a file, importing that into Excel, and saving the Update: |
take |
Thanks @akaihola for the report. can reproduce. on master
expected
|
Looks like there may still be issues here print(pd.__version__)
# 1.5.0.dev0+1099.gd52f2ce0c6
df = pd.DataFrame(
{1: ["value for string index", "value for int index"]}, ["string", 42]
)
with tempfile.TemporaryFile() as fp:
df.to_excel(fp, header=None)
df2 = pd.read_excel(fp, header=None, index_col=0, dtype=str)
print(df2.index)
# Index(['string', 42], dtype='object', name=0)
with tempfile.TemporaryFile() as fp:
df.to_excel(fp, header=None)
df3 = pd.read_excel(fp, header=None, index_col=0, dtype="string")
print(df3.index)
# Index(['string', '42'], dtype='object', name=0) when explicitly specifying when explicitly specifying This is not consistent with the DataFrame and Index constructors... print(pd.DataFrame(["string", 42], dtype=str).dtypes.item())
# object
print(type(pd.DataFrame(["string", 42], dtype=str).iloc[1].item()))
# <class 'str'>
print(pd.DataFrame(["string", 42], dtype="string").dtypes.item())
# string
print(type(pd.DataFrame(["string", 42], dtype="string").iloc[1].item()))
# <class 'str'>
print(pd.Index(["string", 42], dtype=str))
# Index(['string', '42'], dtype='object')
print(pd.Index(["string", 42], dtype="string"))
# Index(['string', '42'], dtype='string') which appear to coerce the values for both |
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
Make sure you have Pandas and
xlrd
installed in a virtualenv, and launch the Python interpreter. You'll needopenpyxl
only if you decide to create the Excel file from Python (see below).python3 -m venv /tmp/pandas-issue-35816 . /tmp/pandas-issue-35816/bin/activate pip install pandas xlrd openpyxl python
To get the Excel files for reproducing the bug, you can either
Details
below into aindex_dtype.csv
file, import that into Excel, and save it in Excel format asindex_dtype.xlsx
, orDetails
below:Finally, copy-paste the following code into Python:
Output:
Problem description
The integer value in the index column appears as an
int
instead of astr
in the index of theDataFrame
read from an Excel file, even thoughdtype=str
was specified.I verified this behavior with spreadsheets
Excel 2007-2019 (.xlsx)
,Excel 97-2003 (.xls)
, and3. create an Excel file from Python
above.Unfortunately I don't have Excel to check if "genuine" Excel spreadsheets cause this as well.
The index dtype is correct if:
read_csv()
is used, orindex_col=0
is omitted (values in the first column all becomestr
)The index dtype is still wrong even if
dtype={0: str, 1: str}
is used to specify the dtype for each column separately.Expected Output
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : d9fff27
python : 3.8.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.11-100.fc29.x86_64
Version : #1 SMP Tue Nov 12 20:41:25 UTC 2019
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.1.0
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.1
setuptools : 49.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: