Skip to content

Commit

Permalink
Fix cudf.Scalar string datetime construction (#9875)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-b-miller authored Dec 15, 2021
1 parent fc2a32a commit 44fce8b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions python/cudf/cudf/tests/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,15 @@ def test_construct_from_scalar(value):

x._is_host_value_current == y._is_host_value_current
x._is_device_value_current == y._is_device_value_current


@pytest.mark.parametrize(
"data", ["20000101", "2000-01-01", "2000-01-01T00:00:00.000000000", "2000"]
)
@pytest.mark.parametrize("dtype", DATETIME_TYPES)
def test_datetime_scalar_from_string(data, dtype):
slr = cudf.Scalar(data, dtype)

expected = np.datetime64(datetime.datetime(2000, 1, 1)).astype(dtype)

assert expected == slr.value
6 changes: 5 additions & 1 deletion python/cudf/cudf/utils/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ def to_cudf_compatible_scalar(val, dtype=None):
val = cudf.api.types.pandas_dtype(type(val)).type(val)

if dtype is not None:
val = val.astype(dtype)
if isinstance(val, str) and np.dtype(dtype).kind == "M":
# pd.Timestamp can handle str, but not np.str_
val = pd.Timestamp(str(val)).to_datetime64().astype(dtype)
else:
val = val.astype(dtype)

if val.dtype.type is np.datetime64:
time_unit, _ = np.datetime_data(val.dtype)
Expand Down

0 comments on commit 44fce8b

Please sign in to comment.