Skip to content

Commit

Permalink
Raise NotImplementedError for to_datetime with z format (#14037)
Browse files Browse the repository at this point in the history
Avoids timezone information from being dropped in `to_datetime` when the z directive is provided

```python
In [1]: import cudf

In [2]: fmt = '%Y-%m-%d %H:%M:%S %Z'
   ...: dates = ['2010-01-01 12:00:00 UTC', '2010-01-01 12:00:00 UTC']

In [3]: cudf.to_datetime(dates, format=fmt)
Out[3]: DatetimeIndex(['2010-01-01 12:00:00', '2010-01-01 12:00:00'], dtype='datetime64[ns]')
```

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

Approvers:
  - Lawrence Mitchell (https://github.com/wence-)

URL: #14037
  • Loading branch information
mroeschke authored Sep 6, 2023
1 parent 609f894 commit ea59dbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions python/cudf/cudf/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,13 @@ def to_datetime(
if utc:
raise NotImplementedError("utc is not yet implemented")

if format is not None and "%f" in format:
format = format.replace("%f", "%9f")
if format is not None:
if "%Z" in format or "%z" in format:
raise NotImplementedError(
"cuDF does not yet support timezone-aware datetimes"
)
elif "%f" in format:
format = format.replace("%f", "%9f")

try:
if isinstance(arg, cudf.DataFrame):
Expand Down
8 changes: 8 additions & 0 deletions python/cudf/cudf/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2148,3 +2148,11 @@ def test_daterange_pandas_compatibility():
"2010-01-01", "2010-02-01", periods=10, name="times"
)
assert_eq(expected, actual)


@pytest.mark.parametrize("code", ["z", "Z"])
def test_format_timezone_not_implemented(code):
with pytest.raises(NotImplementedError):
cudf.to_datetime(
["2020-01-01 00:00:00 UTC"], format=f"%Y-%m-%d %H:%M:%S %{code}"
)

0 comments on commit ea59dbf

Please sign in to comment.