From e0b5daaee714f2f4bd97a869d0b113e0f7bec168 Mon Sep 17 00:00:00 2001 From: galipremsagar Date: Thu, 7 Jan 2021 07:47:35 -0800 Subject: [PATCH] add informative error message for sep --- python/cudf/cudf/io/csv.py | 7 ++++++- python/cudf/cudf/tests/test_csv.py | 24 ++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/python/cudf/cudf/io/csv.py b/python/cudf/cudf/io/csv.py index 2766e0d79d0..fd040181ca8 100644 --- a/python/cudf/cudf/io/csv.py +++ b/python/cudf/cudf/io/csv.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-2021, NVIDIA CORPORATION. from io import BytesIO, StringIO @@ -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() diff --git a/python/cudf/cudf/tests/test_csv.py b/python/cudf/cudf/tests/test_csv.py index 94a5f061383..0ea8b3add4b 100644 --- a/python/cudf/cudf/tests/test_csv.py +++ b/python/cudf/cudf/tests/test_csv.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018-2020, NVIDIA CORPORATION. +# Copyright (c) 2018-2021, NVIDIA CORPORATION. import csv import gzip @@ -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): @@ -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', + )