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

Allow Z in datetime string parsing in non pandas compat mode #14701

Merged
merged 7 commits into from
Jan 6, 2024
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
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