pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
42.59k stars 17.57k forks source link

BUG: index_col in read_csv ignores dtype argument #59077

Open seisman opened 1 week ago

seisman commented 1 week ago

Pandas version checks

Reproducible Example

import io
import numpy as np
import pandas as pd

data = io.StringIO("345.5 519.5 0\n519.5 726.5 1\n")

df = pd.read_csv(
    data,
    sep=r"\s+",
    header=None,
    names=["start", "stop", "bin_id"],
    dtype={"start": np.float32, "stop": np.float32, "bin_id": np.uint32},
    index_col="bin_id"
)
print(df.index.dtype)

Issue Description

df.index.dtype is int64 with pandas 3.0.0.dev0+1132.ga5e812d86d, although the dtype parameter already sets it to np.uint32. The issue is similar to an old issue #9435.

Expected Behavior

Pandas 2.x correctly returns uint32, which is the expected behavior.

Installed Versions

``` INSTALLED VERSIONS ------------------ commit : a5e812d86deb62872f8d514d894a22931fc84217 python : 3.12.3.final.0 python-bits : 64 OS : Linux OS-release : 6.8.11-300.fc40.x86_64 Version : #1 SMP PREEMPT_DYNAMIC Mon May 27 14:53:33 UTC 2024 machine : x86_64 processor : byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 3.0.0.dev0+1132.ga5e812d86d numpy : 1.26.4 pytz : 2024.1 dateutil : 2.9.0.post0 setuptools : 70.0.0 pip : 24.0 Cython : None pytest : 8.2.1 hypothesis : None sphinx : 7.3.7 blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.4 IPython : 8.25.0.dev pandas_datareader : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None bottleneck : None fastparquet : None fsspec : 2024.5.0 gcsfs : None matplotlib : 3.10.0.dev202+gd901275d7c numba : None numexpr : None odfpy : None openpyxl : None pyarrow : 16.1.0 pyreadstat : None python-calamine : None pyxlsb : None s3fs : None scipy : None sqlalchemy : None tables : None tabulate : 0.9.0 xarray : 2024.5.1.dev6+g12123be8 xlrd : None zstandard : None tzdata : 2024.1 qtpy : None pyqt5 : None ```