Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion python/cudf/cudf/io/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import csv
import itertools
import os
from collections.abc import Collection, Mapping
from collections.abc import Collection, Mapping, Sequence
from io import BytesIO, StringIO, TextIOBase
from typing import TYPE_CHECKING, cast

Expand Down Expand Up @@ -366,6 +366,20 @@ def read_csv(
df.index.name = None
elif names is None:
df.index.name = index_col
elif isinstance(index_col, Sequence) and all(
isinstance(col, int) for col in index_col
Comment on lines +366 to +367

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can index_col be a str here? I don't think this is the correct behavior in that case

):
index_col_labels = list(df._data.get_labels_by_index(index_col))
df = df.set_index(index_col_labels)
if names is None:
df.index.names = [
(None if label.startswith("Unnamed:") else label)
if isinstance(label, str) and orig_header == "infer"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
else position
for label, position in zip(
index_col_labels, index_col, strict=True
)
]
else:
df = df.set_index(index_col)

Expand Down
10 changes: 10 additions & 0 deletions python/cudf/cudf/tests/input_output/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,16 @@ def test_csv_reader_index_col():
pd_df = pd.read_csv(StringIO(buffer), header=None, index_col=False)
assert_eq(cu_df.index, pd_df.index)

# using a single column index wrapped in a list
cu_df = read_csv(StringIO(buffer), header=None, index_col=[0])
pd_df = pd.read_csv(StringIO(buffer), header=None, index_col=[0])
assert_eq(cu_df.index, pd_df.index)

# using multiple column indices
cu_df = read_csv(StringIO(buffer), header=None, index_col=[0, 1])
pd_df = pd.read_csv(StringIO(buffer), header=None, index_col=[0, 1])
assert_eq(cu_df.index, pd_df.index)


@pytest.mark.parametrize("index_name", [None, "custom name", 124])
@pytest.mark.parametrize("index_col", [None, 0, "a"])
Expand Down
Loading