-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docstrings and test for strings.convert_durations APIs for pylibc…
…udf (#16982) Contributes to #15162 Since the implementation already existed: * Added docstrings * Like #16971, made the `format` parameter accept `str` instead * Aligned parameter names closer to pylibcudf * Added missing `move`s * Moved `convert_duration` tests to `test_string_convert_duration.py` and added a new test for `from_durations` Authors: - Matthew Roeschke (https://github.com/mroeschke) Approvers: - Vyas Ramasubramani (https://github.com/vyasr) URL: #16982
- Loading branch information
Showing
6 changed files
with
130 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
python/pylibcudf/pylibcudf/tests/test_string_convert_durations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from datetime import datetime, timedelta | ||
|
||
import pyarrow as pa | ||
import pylibcudf as plc | ||
import pytest | ||
from utils import assert_column_eq | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
pa.duration("ns"), | ||
pa.duration("us"), | ||
pa.duration("ms"), | ||
pa.duration("s"), | ||
], | ||
) | ||
def duration_type(request): | ||
return request.param | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def pa_duration_col(): | ||
return pa.array(["05:20:25"]) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def plc_duration_col(pa_duration_col): | ||
return plc.interop.from_arrow(pa_duration_col) | ||
|
||
|
||
def test_to_duration(pa_duration_col, plc_duration_col, duration_type): | ||
format = "%H:%M:%S" | ||
|
||
def to_timedelta(duration_str): | ||
date = datetime.strptime(duration_str, format) | ||
return date - datetime(1900, 1, 1) # "%H:%M:%S" zero date | ||
|
||
expect = pa.array([to_timedelta(d.as_py()) for d in pa_duration_col]).cast( | ||
duration_type | ||
) | ||
|
||
got = plc.strings.convert.convert_durations.to_durations( | ||
plc_duration_col, | ||
plc.interop.from_arrow(duration_type), | ||
format, | ||
) | ||
assert_column_eq(expect, got) | ||
|
||
|
||
@pytest.mark.parametrize("format", [None, "%D days %H:%M:%S"]) | ||
def test_from_durations(format): | ||
pa_array = pa.array( | ||
[timedelta(days=1, hours=1, minutes=1, seconds=1), None] | ||
) | ||
result = plc.strings.convert.convert_durations.from_durations( | ||
plc.interop.from_arrow(pa_array), format | ||
) | ||
expected = pa.array(["1 days 01:01:01", None]) | ||
assert_column_eq(result, expected) |