Skip to content

Commit

Permalink
Allow Z in datetime string parsing in non pandas compat mode (#14701)
Browse files Browse the repository at this point in the history
xref #14039 (comment)

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - Christopher Harris (https://github.com/cwharris)
  - Bradley Dice (https://github.com/bdice)

URL: #14701
  • Loading branch information
mroeschke authored Jan 6, 2024
1 parent 0c98134 commit 6083efa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 4 additions & 1 deletion python/cudf/cudf/core/column/datetime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2023, NVIDIA CORPORATION.
# Copyright (c) 2019-2024, NVIDIA CORPORATION.

from __future__ import annotations

Expand Down Expand Up @@ -102,6 +102,9 @@ def infer_format(element: str, **kwargs) -> str:
"""
Infers datetime format from a string, also takes cares for `ms` and `ns`
"""
if not cudf.get_option("mode.pandas_compatible"):
# We allow "Z" but don't localize it to datetime64[ns, UTC] type (yet)
element = element.replace("Z", "")
fmt = _guess_datetime_format(element, **kwargs)

if fmt is not None:
Expand Down
22 changes: 17 additions & 5 deletions python/cudf/cudf/tests/test_datetime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2023, NVIDIA CORPORATION.
# Copyright (c) 2019-2024, NVIDIA CORPORATION.

import datetime
import operator
Expand Down Expand Up @@ -1306,8 +1306,10 @@ def test_datetime_infer_format(data, timezone, dtype):

assert_eq(expected, actual)
else:
with pytest.raises(NotImplementedError):
sr.astype(dtype)
with cudf.option_context("mode.pandas_compatible", True):
with pytest.raises(NotImplementedError):
# pandas doesn't allow parsing "Z" to naive type
sr.astype(dtype)


def test_dateoffset_instance_subclass_check():
Expand Down Expand Up @@ -2308,12 +2310,22 @@ def test_format_timezone_not_implemented(code):
)


@pytest.mark.parametrize("tz", ["Z", "UTC-3", "+01:00"])
def test_no_format_timezone_not_implemented(tz):
@pytest.mark.parametrize("tz", ["UTC-3", "+01:00"])
def test_utc_offset_not_implemented(tz):
with pytest.raises(NotImplementedError):
cudf.to_datetime([f"2020-01-01 00:00:00{tz}"])


def test_Z_utc_offset():
with cudf.option_context("mode.pandas_compatible", True):
with pytest.raises(NotImplementedError):
cudf.to_datetime(["2020-01-01 00:00:00Z"])

result = cudf.to_datetime(["2020-01-01 00:00:00Z"])
expected = cudf.to_datetime(["2020-01-01 00:00:00"])
assert_eq(result, expected)


@pytest.mark.parametrize("arg", [True, False])
def test_args_not_datetime_typerror(arg):
with pytest.raises(TypeError):
Expand Down

0 comments on commit 6083efa

Please sign in to comment.