Skip to content

BUG: Fix read_csv index dtype override #59219

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ I/O
- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Bug in :meth:`HDFStore.get` was failing to save data of dtype datetime64[s] correctly (:issue:`59004`)
- Bug in :meth:`read_csv` causing segmentation fault when ``encoding_errors`` is not a string. (:issue:`59059`)
- Bug in :meth:`read_csv` not respecting ``dtype`` for ``index``. (:issue:`59077`)
- Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)
- Bug in :meth:`read_csv` raising ``TypeError`` when ``nrows`` and ``iterator`` are specified without specifying a ``chunksize``. (:issue:`59079`)
- Bug in :meth:`read_excel` raising ``ValueError`` when passing array of boolean values when ``dtype="boolean"``. (:issue:`58159`)
Expand Down
17 changes: 13 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6379,9 +6379,11 @@ def _transform_index(self, func, *, level=None) -> Index:
"""
if isinstance(self, ABCMultiIndex):
values = [
self.get_level_values(i).map(func)
if i == level or level is None
else self.get_level_values(i)
(
self.get_level_values(i).map(func)
if i == level or level is None
else self.get_level_values(i)
)
for i in range(self.nlevels)
]
return type(self).from_arrays(values)
Expand Down Expand Up @@ -7478,7 +7480,14 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:
if len(sequences) == 1:
if names is not None:
names = names[0]
return Index(maybe_sequence_to_range(sequences[0]), name=names)
data = sequences[0]
conv_data = maybe_sequence_to_range(data)
dtype = (
data.dtype
if isinstance(data, np.ndarray) and isinstance(conv_data, range)
else None
)
return Index(conv_data, dtype=dtype, name=names)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#59077 (comment)
I think this is the best place to add in the explicit dtype after the possible conversion to python native range type.

else:
# TODO: Apply maybe_sequence_to_range to sequences?
return MultiIndex.from_arrays(sequences, names=names)
Expand Down
10 changes: 7 additions & 3 deletions pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,17 @@ def read(
data, columns = self._exclude_implicit_index(alldata)

conv_data = self._convert_data(data)
conv_data = self._do_date_conversions(columns, conv_data)
date_data = self._do_date_conversions(columns, conv_data)

if not self._implicit_index:
# propagate index dtype
alldata = list(conv_data.values()) # type: ignore[arg-type]

index, result_columns = self._make_index(
conv_data, alldata, columns, indexnamerow
date_data, alldata, columns, indexnamerow
)

return index, result_columns, conv_data
return index, result_columns, date_data

def _exclude_implicit_index(
self,
Expand Down
21 changes: 14 additions & 7 deletions pandas/tests/io/parser/dtypes/test_dtypes_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,25 @@ def test_dtype_all_columns(all_parsers, dtype, check_orig):
def test_dtype_per_column(all_parsers):
parser = all_parsers
data = """\
one,two
1,2.5
2,3.5
3,4.5
4,5.5"""
one,two,three
1,2.5,11
2,3.5,12
3,4.5,13
4,5.5,14"""
expected = DataFrame(
[[1, "2.5"], [2, "3.5"], [3, "4.5"], [4, "5.5"]], columns=["one", "two"]
[[1, "2.5", 11], [2, "3.5", 12], [3, "4.5", 13], [4, "5.5", 14]],
columns=["one", "two", "three"],
)
expected["one"] = expected["one"].astype(np.float64)
expected["two"] = expected["two"].astype(object)
expected["three"] = expected["three"].astype(np.uint32)
expected.set_index("three", inplace=True)

result = parser.read_csv(StringIO(data), dtype={"one": np.float64, 1: str})
result = parser.read_csv(
StringIO(data),
dtype={"one": np.float64, 1: str, "three": np.uint32},
index_col="three",
)
tm.assert_frame_equal(result, expected)


Expand Down