Skip to content

Commit

Permalink
Add informative error message for sep in CSV writer(#7095)
Browse files Browse the repository at this point in the history
Fixes: #7091 

This PR introduces validation and throwing of informative error messages for the `sep` parameter in csv writer.

Authors:
  - galipremsagar <[email protected]>

Approvers:
  - Keith Kraus (@kkraus14)
  - Vukasin Milovanovic (@vuule)

URL: #7095
  • Loading branch information
galipremsagar authored Jan 7, 2021
1 parent ee65a47 commit aa38f85
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 aa38f85

Please sign in to comment.