Skip to content

Commit

Permalink
add informative error message for sep
Browse files Browse the repository at this point in the history
  • Loading branch information
galipremsagar committed Jan 7, 2021
1 parent f768da7 commit e0b5daa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 6 additions & 1 deletion python/cudf/cudf/io/csv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2020, NVIDIA CORPORATION.
# Copyright (c) 2018-2021, NVIDIA CORPORATION.

from io import BytesIO, StringIO

Expand Down Expand Up @@ -119,6 +119,11 @@ def to_csv(
):
"""{docstring}"""

if not isinstance(sep, str):
raise TypeError(f'"sep" must be string, not {type(sep).__name__}')
elif len(sep) > 1:
raise TypeError('"sep" must be a 1-character string')

return_as_string = False
if path_or_buf is None:
path_or_buf = StringIO()
Expand Down
24 changes: 22 additions & 2 deletions python/cudf/cudf/tests/test_csv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2020, NVIDIA CORPORATION.
# Copyright (c) 2018-2021, NVIDIA CORPORATION.

import csv
import gzip
Expand All @@ -15,7 +15,7 @@

import cudf
from cudf import read_csv
from cudf.tests.utils import assert_eq
from cudf.tests.utils import assert_eq, assert_exceptions_equal


def make_numeric_dataframe(nrows, dtype):
Expand Down Expand Up @@ -1935,3 +1935,23 @@ def test_na_filter_empty_fields():
StringIO(buffer), keep_default_na=False, na_values=test_na
)
assert_eq(pdf, gdf)


def test_csv_sep_error():
pdf = pd.DataFrame({"a": [1, 2, 3]})
gdf = cudf.DataFrame({"a": [1, 2, 3]})
assert_exceptions_equal(
lfunc=pdf.to_csv,
rfunc=gdf.to_csv,
lfunc_args_and_kwargs=([], {"sep": "abc"}),
rfunc_args_and_kwargs=([], {"sep": "abc"}),
expected_error_message='"sep" must be a 1-character string',
)

assert_exceptions_equal(
lfunc=pdf.to_csv,
rfunc=gdf.to_csv,
lfunc_args_and_kwargs=([], {"sep": 1}),
rfunc_args_and_kwargs=([], {"sep": 1}),
expected_error_message='"sep" must be string, not int',
)

0 comments on commit e0b5daa

Please sign in to comment.