Skip to content
Open
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
21 changes: 16 additions & 5 deletions 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 @@ -177,9 +177,6 @@ def read_csv(
if byte_range is None:
byte_range = (0, 0)

# We need this later when setting index cols
orig_header = header

if names is not None:
# explicitly mentioned name, so don't check header
if header is None or header == "infer":
Expand Down Expand Up @@ -358,14 +355,28 @@ def read_csv(
if (
isinstance(index_col_name, str)
and names is None
and orig_header == "infer"
and header != -1
):
if index_col_name.startswith("Unnamed:"):
# TODO: Try to upstream it to libcudf
# csv reader in future
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 header != -1
else position
for label, position in zip(
index_col_labels, index_col, strict=True
)
]
else:
df = df.set_index(index_col)

Expand Down
22 changes: 22 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,28 @@ 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)


def test_csv_reader_index_col_position_with_explicit_header():
buffer = "a,b,c\n3,4,5\n6,7,8"

cu_df = read_csv(StringIO(buffer), header=0, index_col=[0])
pd_df = pd.read_csv(StringIO(buffer), header=0, index_col=[0])
assert_eq(cu_df.index, pd_df.index)

cu_df = read_csv(StringIO(buffer), header=0, index_col=0)
pd_df = pd.read_csv(StringIO(buffer), header=0, index_col=0)
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