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

Raise error when trying to construct time-zone aware timestamps #13830

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 11 additions & 5 deletions python/cudf/cudf/core/column/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,20 @@ def normalize_binop_value(self, other: DatetimeLikeScalar) -> ScalarLike:
if isinstance(other, (cudf.Scalar, ColumnBase, cudf.DateOffset)):
return other

if isinstance(other, datetime.datetime):
other = np.datetime64(other)
elif isinstance(other, datetime.timedelta):
other = np.timedelta64(other)
elif isinstance(other, pd.Timestamp):
if isinstance(other, pd.Timestamp):
if other.tz is not None:
raise TypeError(
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
"Cannot perform binary operation on timezone-naive columns"
" and timezone-aware timestamps. For that, "
"use `tz_localize`."
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
)
other = other.to_datetime64()
elif isinstance(other, pd.Timedelta):
other = other.to_timedelta64()
elif isinstance(other, datetime.datetime):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about TZ-aware datetime.datetime objects? e.g.

>>> import datetime
>>> datetime.datetime.now(datetime.timezone.utc)
datetime.datetime(2023, 8, 7, 20, 47, 48, 483493, tzinfo=datetime.timezone.utc)

Do those need to error, too? On conversion to np.datetime64, I get a warning. Is that sufficient?

>>> np.datetime64(datetime.datetime(2023, 8, 7, 20, 47, 48, 483493, tzinfo=datetime.timezone.utc))
<stdin>:1: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
numpy.datetime64('2023-08-07T20:47:48.483493')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should raise for it too. Good catch, I thought datetime was tz agnostic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdice I updated the code to handle datetime.datetime objects aswell.

other = np.datetime64(other)
elif isinstance(other, datetime.timedelta):
other = np.timedelta64(other)

if isinstance(other, np.datetime64):
if np.isnat(other):
Expand Down
14 changes: 11 additions & 3 deletions python/cudf/cudf/core/column/timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,20 @@ def _binaryop(self, other: ColumnBinaryOperand, op: str) -> ColumnBase:
def normalize_binop_value(self, other) -> ColumnBinaryOperand:
if isinstance(other, (ColumnBase, cudf.Scalar)):
return other
if isinstance(other, datetime.timedelta):
other = np.timedelta64(other)
elif isinstance(other, pd.Timestamp):

if isinstance(other, pd.Timestamp):
if other.tz is not None:
raise TypeError(
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
"Cannot perform binary operation on timezone-naive columns"
" and timezone-aware timestamps. For that, "
"use `tz_localize`."
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
)
other = other.to_datetime64()
elif isinstance(other, pd.Timedelta):
other = other.to_timedelta64()
elif isinstance(other, datetime.timedelta):
other = np.timedelta64(other)

if isinstance(other, np.timedelta64):
other_time_unit = cudf.utils.dtypes.get_time_unit(other)
if np.isnat(other):
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 @@ -2093,3 +2093,11 @@ def test_construction_from_tz_timestamps(data):
_ = cudf.Series(data)
with pytest.raises(NotImplementedError):
_ = cudf.Index(data)


@pytest.mark.parametrize("op", _cmpops)
def test_datetime_binop_tz_timestamp(op):
s = cudf.Series([1, 2, 3], dtype="datetime64[ns]")
pd_tz_timestamp = pd.Timestamp("1970-01-01 00:00:00.000000001", tz="utc")
with pytest.raises(TypeError):
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
op(s, pd_tz_timestamp)
8 changes: 7 additions & 1 deletion python/cudf/cudf/tests/test_scalar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021-2022, NVIDIA CORPORATION.
# Copyright (c) 2021-2023, NVIDIA CORPORATION.

import datetime
import re
Expand Down Expand Up @@ -450,3 +450,9 @@ def test_scalar_numpy_casting():
s1 = cudf.Scalar(1, dtype=np.int32)
s2 = np.int64(2)
assert s1 < s2


def test_construct_timezone_scalar_error():
pd_scalar = pd.Timestamp("1970-01-01 00:00:00.000000001", tz="utc")
with pytest.raises(TypeError):
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
cudf.utils.dtypes.to_cudf_compatible_scalar(pd_scalar)
8 changes: 8 additions & 0 deletions python/cudf/cudf/tests/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,3 +1426,11 @@ def test_timedelta_constructor(data, dtype):
actual = cudf.TimedeltaIndex(data=cudf.Series(data), dtype=dtype)

assert_eq(expected, actual)


@pytest.mark.parametrize("op", [operator.add, operator.sub])
def test_timdelta_binop_tz_timestamp(op):
s = cudf.Series([1, 2, 3], dtype="timedelta64[ns]")
pd_tz_timestamp = pd.Timestamp("1970-01-01 00:00:00.000000001", tz="utc")
with pytest.raises(TypeError):
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
op(s, pd_tz_timestamp)
16 changes: 11 additions & 5 deletions python/cudf/cudf/utils/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,20 @@ def to_cudf_compatible_scalar(val, dtype=None):
# the string value directly (cudf.DeviceScalar will DTRT)
return val

if isinstance(val, datetime.datetime):
val = np.datetime64(val)
elif isinstance(val, datetime.timedelta):
val = np.timedelta64(val)
elif isinstance(val, pd.Timestamp):
if isinstance(val, pd.Timestamp):
if val.tz is not None:
raise TypeError(
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
"Cannot covert a timezone-aware timestamp to"
" timezone-naive scalar. For that, "
"use `tz_localize`."
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
)
val = val.to_datetime64()
elif isinstance(val, pd.Timedelta):
val = val.to_timedelta64()
elif isinstance(val, datetime.datetime):
val = np.datetime64(val)
elif isinstance(val, datetime.timedelta):
val = np.timedelta64(val)

val = _maybe_convert_to_default_type(
cudf.api.types.pandas_dtype(type(val))
Expand Down