You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importpandasaspdimportnumpyasnpfrompathlibimportPath# Create a simple df with a RangeIndex for the columns. N.B. The RangeIndex# start and stop have the same sign.range_idx=pd.RangeIndex(start=-10, stop=-5, step=1)
df=pd.DataFrame(np.random.randn(3, len(range_idx)), columns=range_idx)
csv_file=Path("./data/test_range_index.csv")
csv_file.parent.mkdir(exist_ok=True)
# Write df to a CSV along with row+col indicesdf.to_csv(
csv_file,
header=True,
index=True,
)
# Create a dictionary to specify the dtypesdtype_spec= {}
forvinrange_idx:
dtype_spec[v] ="float64"# dtype_spec[f"{v}"] = "float64" # * Alternatively, convert to str, which works# Reload the CSVdf_reload=pd.read_csv(
csv_file,
header=0,
index_col=0,
float_precision='round_trip',
dtype=dtype_spec,
)
Issue Description
This may in part be my misunderstanding of how dtypes should be specified and the implicit conversion of the column index to str.
Notwithstanding that, the problem is when reading a CSV using read_csv() with a header row that consists of integers of the same sign and specifying the dtype with a dictionary that has integer keys: it fails with an IndexError: list index out of range.
The IndexError does not get raised if the original RangeIndex has a start and stop of different signs.
Neither does it occur if the dtype= is not specified in read_csv()'s arguments or if the keys in the dictionary of the dtypes are converted to str.
In all successful cases, the columns are an Index that contain the str representation of the integers. Does this mean they should always be integers for the purposes of read_csv() and that any conversion back to integer has to happen later? If so, the docs might benefit from being a little bit clearer.
Expected Behavior
That read_csv() would succeed when specifying the dtypes with a dict with integer keys (of the same sign).
Installed Versions
INSTALLED VERSIONS
commit : bdc79c1
python : 3.11.8.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-101-generic
Version : #111~20.04.1-Ubuntu SMP Mon Mar 11 15:44:43 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
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
Issue Description
This may in part be my misunderstanding of how dtypes should be specified and the implicit conversion of the column index to str.
Notwithstanding that, the problem is when reading a CSV using
read_csv()
with a header row that consists of integers of the same sign and specifying the dtype with a dictionary that has integer keys: it fails with anIndexError: list index out of range
.Traceback
Further discussion
The
IndexError
does not get raised if the originalRangeIndex
has a start and stop of different signs.Neither does it occur if the
dtype=
is not specified inread_csv()
's arguments or if the keys in the dictionary of the dtypes are converted to str.In all successful cases, the columns are an
Index
that contain the str representation of the integers. Does this mean they should always be integers for the purposes ofread_csv()
and that any conversion back to integer has to happen later? If so, the docs might benefit from being a little bit clearer.Expected Behavior
That
read_csv()
would succeed when specifying the dtypes with a dict with integer keys (of the same sign).Installed Versions
INSTALLED VERSIONS
commit : bdc79c1
python : 3.11.8.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-101-generic
Version : #111~20.04.1-Ubuntu SMP Mon Mar 11 15:44:43 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0
setuptools : 69.2.0
pip : 24.0
Cython : None
pytest : 8.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.22.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : 2024.2.0
fsspec : 2024.3.1
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.9.0
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 15.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : 2.0.1
zstandard : 0.22.0
tzdata : 2024.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: