From c03d95d501927e226fe36094d6ef4c0f1fb9de2f Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 6 Sep 2023 13:52:36 -0700 Subject: [PATCH] Raise TypeError for any non-parseable argument in to_datetime --- python/cudf/cudf/core/tools/datetimes.py | 16 ++++++++-------- python/cudf/cudf/tests/test_datetime.py | 6 ++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/python/cudf/cudf/core/tools/datetimes.py b/python/cudf/cudf/core/tools/datetimes.py index a759f9dc3e1..f736e055163 100644 --- a/python/cudf/cudf/core/tools/datetimes.py +++ b/python/cudf/cudf/core/tools/datetimes.py @@ -294,12 +294,8 @@ def to_datetime( def _process_col(col, unit, dayfirst, infer_datetime_format, format): if col.dtype.kind == "M": return col - elif col.dtype.kind == "m": - raise TypeError( - f"dtype {col.dtype} cannot be converted to {_unit_dtype_map[unit]}" - ) - if col.dtype.kind in ("f"): + elif col.dtype.kind in ("f"): if unit not in (None, "ns"): factor = cudf.Scalar( column.datetime._unit_to_nanoseconds_conversion[unit] @@ -325,8 +321,9 @@ def _process_col(col, unit, dayfirst, infer_datetime_format, format): ) else: col = col.as_datetime_column(dtype="datetime64[ns]") + return col - if col.dtype.kind in ("i"): + elif col.dtype.kind in ("i"): if unit in ("D", "h", "m"): factor = cudf.Scalar( column.datetime._unit_to_nanoseconds_conversion[unit] @@ -340,6 +337,7 @@ def _process_col(col, unit, dayfirst, infer_datetime_format, format): ) else: col = col.as_datetime_column(dtype=_unit_dtype_map[unit]) + return col elif col.dtype.kind in ("O"): if unit not in (None, "ns") or col.null_count == len(col): @@ -364,11 +362,13 @@ def _process_col(col, unit, dayfirst, infer_datetime_format, format): format = column.datetime.infer_format( element=col.element_indexing(0) ) - col = col.as_datetime_column( + return col.as_datetime_column( dtype=_unit_dtype_map[unit], format=format, ) - return col + raise TypeError( + f"dtype {col.dtype} cannot be converted to {_unit_dtype_map[unit]}" + ) def get_units(value): diff --git a/python/cudf/cudf/tests/test_datetime.py b/python/cudf/cudf/tests/test_datetime.py index 4a4e9b67c2e..4c20258ae67 100644 --- a/python/cudf/cudf/tests/test_datetime.py +++ b/python/cudf/cudf/tests/test_datetime.py @@ -2156,3 +2156,9 @@ def test_format_timezone_not_implemented(code): cudf.to_datetime( ["2020-01-01 00:00:00 UTC"], format=f"%Y-%m-%d %H:%M:%S %{code}" ) + + +@pytest.mark.parametrize("arg", [True, False]) +def test_args_not_datetime_typerror(arg): + with pytest.raises(TypeError): + cudf.to_datetime([arg])