From 406e87bdb8b1976bc8d47794b79901fc35d4803d Mon Sep 17 00:00:00 2001 From: Marlene <57748216+marlenezw@users.noreply.github.com> Date: Mon, 23 Aug 2021 17:40:17 +0200 Subject: [PATCH] Allowing %f in format to return nanoseconds (#9081) This is a quick fix to close PR #7945 This PR checks to see if `%f` is passed as part of `format` into `cudf.to_datetime`. Previously, cudf would not return nanoseconds, while pandas does. Authors: - Marlene (https://github.com/marlenezw) Approvers: - Ashwin Srinath (https://github.com/shwina) URL: https://github.com/rapidsai/cudf/pull/9081 --- python/cudf/cudf/core/tools/datetimes.py | 3 +++ python/cudf/cudf/tests/test_datetime.py | 1 + 2 files changed, 4 insertions(+) diff --git a/python/cudf/cudf/core/tools/datetimes.py b/python/cudf/cudf/core/tools/datetimes.py index 946cdcb1ebc..4856995b391 100644 --- a/python/cudf/cudf/core/tools/datetimes.py +++ b/python/cudf/cudf/core/tools/datetimes.py @@ -123,6 +123,9 @@ def to_datetime( if yearfirst: raise NotImplementedError("yearfirst support is not yet implemented") + if format is not None and "%f" in format: + format = format.replace("%f", "%9f") + try: if isinstance(arg, cudf.DataFrame): # we require at least Ymd diff --git a/python/cudf/cudf/tests/test_datetime.py b/python/cudf/cudf/tests/test_datetime.py index 9f19bf8b960..65e87e88f55 100644 --- a/python/cudf/cudf/tests/test_datetime.py +++ b/python/cudf/cudf/tests/test_datetime.py @@ -717,6 +717,7 @@ def test_to_datetime_units(data, unit): (["10/11/2012", "01/01/2010", "07/07/2016", "02/02/2014"], "%m/%d/%Y"), (["10/11/2012", "01/01/2010", "07/07/2016", "02/02/2014"], "%d/%m/%Y"), (["10/11/2012", "01/01/2010", "07/07/2016", "02/02/2014"], None), + (["2021-04-13 12:30:04.123456789"], "%Y-%m-%d %H:%M:%S.%f"), (pd.Series([2015, 2020, 2021]), "%Y"), pytest.param( pd.Series(["1", "2", "1"]),