Skip to content

IO Conflict Between index_col and dtype #25067

Closed
@mbkupfer

Description

@mbkupfer

Code Sample, a copy-pastable example if possible

I'm reading in an excel file using the code below. Assume that column 0 is my zip codes and column 2 is my adjustment factor that I'm mapping zips to.

# Your code here
 zip_adjustments_factors = pd.read_excel(fp, sheet_name='zip_mapping', 
    dtype={'zip': object}, index_col=0, usecols=[0,2])

In [1]: zip_adjustments_factors.index.dtype
Out[1]: dtype('int64')

Problem description

In my situation, I need zipcodes to be dtype of object since zipcodes can start with 0's, but the converter under-the-hood is converting these to int's and dropping the 0's.

This requires an extra step as a workaround:

zip_adjustments_factors = pd.read_excel(fp, sheet_name='zip_mapping', 
    dtype={'zip': object}, usecols=[0,2])
zip_adjustments_factors.set_index('zip')

In [1]: zip_adjustments_factors.set_index('zip').index.dtype
Out[1]: dtype('O')

But couldn't this be done within pd.read_excel by checking whether the index column parameter has a specified dtype parameter?

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.4.final.0
python-bits: 32
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.4
pytest: 3.7.1
pip: 18.1
setuptools: 40.5.0
Cython: None
numpy: 1.15.2
scipy: 1.2.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: 2.5.5
xlrd: 1.1.0
xlwt: None
xlsxwriter: 1.1.2
lxml: 4.1.1
bs4: 4.6.1
html5lib: 1.0.1
sqlalchemy: 1.2.12
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate ReportDuplicate issue or pull requestIO DataIO issues that don't fit into a more specific label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions