Skip to content

BUG: Inconsistent index type when using read_csv with string[pyarrow] dtype #61145

Open
@ptth222

Description

@ptth222

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

df = pandas.DataFrame([['a', 'b'], ['c', 'd']])
path = 'test.csv'
df.to_csv(path)

df = pandas.read_csv(path, dtype='string[pyarrow]', index_col=0)
df.index # Index dtype is string.

df = pandas.read_csv(path, dtype=str, index_col=0)
df.index # Index dtype is int64.

Issue Description

I'm not sure if this is intended or not. If you use the index_col parameter in read_csv with the 'string[pyarrow]' dtype the index dtype is string, but if you use the str dtype or don't specify a dtype it will be int64. It has been my experience with pandas that the dtype only refers to the data and not the index. I initially had a few bugs stemming from switching to the 'string[pyarrow]' dtype and the resulting index type change. I also tested read_excel, and it has an index with an int64 dtype when specifying a 'string[pyarrow]' dtype, so there is also inconsistency when reading different table formats.

Expected Behavior

The expected behavior is that the index is the same dtype regardless of the specified dtype in read_csv, or that the index dtype is always affected by the specified dtype, and that this behavior is the same for read_excel.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.10.5
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en
LOCALE : English_United States.1252

pandas : 2.2.3
numpy : 1.24.4
pytz : 2022.1
dateutil : 2.8.2
pip : 25.0.1
Cython : 3.0.11
sphinx : 5.1.1
IPython : 8.21.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : 1.1
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : 4.9.1
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.4
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.9
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : 3.2.0
zstandard : None
tzdata : 2024.1
qtpy : 2.4.1
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    API - ConsistencyInternal Consistency of API/BehaviorBugIO CSVread_csv, to_csvNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions