-
Notifications
You must be signed in to change notification settings - Fork 912
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
Add string.convert.convert_fixed_type APIs to pylibcudf #16984
Merged
rapids-bot
merged 3 commits into
rapidsai:branch-24.12
from
mroeschke:pylibcudf/strings/convert_fixed_point
Oct 4, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
from . import convert_datetime, convert_durations | ||
from . import convert_datetime, convert_durations, convert_fixed_point |
11 changes: 11 additions & 0 deletions
11
python/pylibcudf/pylibcudf/strings/convert/convert_fixed_point.pxd
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,11 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from pylibcudf.column cimport Column | ||
from pylibcudf.types cimport DataType | ||
|
||
|
||
cpdef Column to_fixed_point(Column input, DataType output_type) | ||
|
||
cpdef Column from_fixed_point(Column input) | ||
|
||
cpdef Column is_fixed_point(Column input, DataType decimal_type=*) |
107 changes: 107 additions & 0 deletions
107
python/pylibcudf/pylibcudf/strings/convert/convert_fixed_point.pyx
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,107 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.utility cimport move | ||
from pylibcudf.column cimport Column | ||
from pylibcudf.libcudf.column.column cimport column | ||
from pylibcudf.libcudf.strings.convert cimport ( | ||
convert_fixed_point as cpp_fixed_point, | ||
) | ||
from pylibcudf.types cimport DataType, type_id | ||
|
||
|
||
cpdef Column to_fixed_point(Column input, DataType output_type): | ||
""" | ||
Returns a new fixed-point column parsing decimal values from the | ||
provided strings column. | ||
|
||
For details, see :cpp:details:`cudf::strings::to_fixed_point` | ||
|
||
Parameters | ||
---------- | ||
input : Column | ||
Strings instance for this operation. | ||
|
||
output_type : DataType | ||
Type of fixed-point column to return including the scale value. | ||
|
||
Returns | ||
------- | ||
Column | ||
New column of output_type. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_fixed_point.to_fixed_point( | ||
input.view(), | ||
output_type.c_obj, | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
cpdef Column from_fixed_point(Column input): | ||
""" | ||
Returns a new strings column converting the fixed-point values | ||
into a strings column. | ||
|
||
For details, see :cpp:details:`cudf::strings::from_fixed_point` | ||
|
||
Parameters | ||
---------- | ||
input : Column | ||
Fixed-point column to convert. | ||
|
||
Returns | ||
------- | ||
Column | ||
New strings column. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_fixed_point.from_fixed_point( | ||
input.view(), | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
cpdef Column is_fixed_point(Column input, DataType decimal_type=None): | ||
""" | ||
Returns a boolean column identifying strings in which all | ||
characters are valid for conversion to fixed-point. | ||
|
||
For details, see :cpp:details:`cudf::strings::is_fixed_point` | ||
|
||
Parameters | ||
---------- | ||
input : Column | ||
Strings instance for this operation. | ||
|
||
decimal_type : DataType | ||
Fixed-point type (with scale) used only for checking overflow. | ||
Defaults to Decimal64 | ||
|
||
Returns | ||
------- | ||
Column | ||
New column of boolean results for each string. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
if decimal_type is None: | ||
decimal_type = DataType(type_id.DECIMAL64) | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_fixed_point.is_fixed_point( | ||
input.view(), | ||
decimal_type.c_obj, | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) |
34 changes: 34 additions & 0 deletions
34
python/pylibcudf/pylibcudf/tests/test_string_convert_fixed_point.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,34 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
import decimal | ||
|
||
import pyarrow as pa | ||
import pylibcudf as plc | ||
from utils import assert_column_eq | ||
|
||
|
||
def test_to_fixed_point(): | ||
typ = pa.decimal128(38, 2) | ||
arr = pa.array(["123", "1.23", None]) | ||
result = plc.strings.convert.convert_fixed_point.to_fixed_point( | ||
plc.interop.from_arrow(arr), plc.interop.from_arrow(typ) | ||
) | ||
expected = arr.cast(typ) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
def test_from_fixed_point(): | ||
arr = pa.array([decimal.Decimal("1.1"), None]) | ||
result = plc.strings.convert.convert_fixed_point.from_fixed_point( | ||
plc.interop.from_arrow(arr), | ||
) | ||
expected = pa.array(["1.1", None]) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
def test_is_fixed_point(): | ||
arr = pa.array(["123", "1.23", "1.2.3", "", None]) | ||
result = plc.strings.convert.convert_fixed_point.is_fixed_point( | ||
plc.interop.from_arrow(arr), | ||
) | ||
expected = pa.array([True, True, False, False, None]) | ||
assert_column_eq(result, expected) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a corresponding pxd file? If not, can we add it? If so, can we update it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes we have one and forgot to update.