Skip to content

Commit 398c943

Browse files
committed
Preserve header-only CSVs in pandas-compatible mode
1 parent 3b3cea4 commit 398c943

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

python/cudf/cudf/io/csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def read_csv(
274274
table_w_meta = plc.io.csv.read_csv(options)
275275
df = DataFrame.from_pylibcudf(table_w_meta)
276276

277-
if get_option("mode.pandas_compatible") and df.empty:
277+
if get_option("mode.pandas_compatible") and len(df._column_names) == 0:
278278
raise pd.errors.EmptyDataError("No columns to parse from file")
279279

280280
# Cast result to categorical if specified in dtype=

python/cudf/cudf/tests/input_output/test_csv.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,21 @@ def test_empty_file_pandas_compat_raises(tmp_path):
22272227
cudf.read_csv(str(empty_file))
22282228

22292229

2230+
@pytest.mark.parametrize(
2231+
"buffer,kwargs",
2232+
[
2233+
("a,b\n", {}),
2234+
("", {"names": ["a", "b"]}),
2235+
],
2236+
)
2237+
def test_empty_csv_with_columns_pandas_compat(buffer, kwargs):
2238+
with cudf.option_context("mode.pandas_compatible", True):
2239+
got = cudf.read_csv(StringIO(buffer), **kwargs)
2240+
2241+
expect = pd.read_csv(StringIO(buffer), **kwargs)
2242+
assert_eq(expect, got)
2243+
2244+
22302245
def test_read_csv_gcs(monkeypatch):
22312246
gcsfs = pytest.importorskip("gcsfs")
22322247
pdf = pd.DataFrame(

0 commit comments

Comments
 (0)