Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REVIEW] Add informative error message for sep in CSV writer #7095

Merged
merged 1 commit into from
Jan 7, 2021
Merged
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
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',
)