-
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 string.attributes APIs to pylibcudf (#16785)
Contributes to #15162 Authors: - Matthew Roeschke (https://github.com/mroeschke) - Matthew Murray (https://github.com/Matt711) Approvers: - Matthew Murray (https://github.com/Matt711) - Vyas Ramasubramani (https://github.com/vyasr) URL: #16785
- Loading branch information
Showing
7 changed files
with
185 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from pylibcudf.column cimport Column | ||
|
||
|
||
cpdef Column count_characters(Column source_strings) | ||
|
||
cpdef Column count_bytes(Column source_strings) | ||
|
||
cpdef Column code_points(Column source_strings) |
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,76 @@ | ||
# 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 cimport attributes as cpp_attributes | ||
|
||
|
||
cpdef Column count_characters(Column source_strings): | ||
""" | ||
Returns a column containing character lengths of each string | ||
in the given column. | ||
Parameters | ||
---------- | ||
source_strings : Column | ||
Column of strings. | ||
Returns | ||
------- | ||
Column | ||
New column with lengths for each string | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move(cpp_attributes.count_characters(source_strings.view())) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column count_bytes(Column source_strings): | ||
""" | ||
Returns a column containing byte lengths of each string | ||
in the given column. | ||
Parameters | ||
---------- | ||
source_strings : Column | ||
Column of strings. | ||
Returns | ||
------- | ||
Column | ||
New column with the number of bytes for each string | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move(cpp_attributes.count_bytes(source_strings.view())) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column code_points(Column source_strings): | ||
""" | ||
Creates a numeric column with code point values (integers) | ||
for each character of each string. | ||
Parameters | ||
---------- | ||
source_strings : Column | ||
Column of strings. | ||
Returns | ||
------- | ||
Column | ||
New column with code point integer values for each character | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move(cpp_attributes.code_points(source_strings.view())) | ||
|
||
return Column.from_libcudf(move(c_result)) |
32 changes: 32 additions & 0 deletions
32
python/pylibcudf/pylibcudf/tests/test_string_attributes.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,32 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import pyarrow as pa | ||
import pyarrow.compute as pc | ||
import pylibcudf as plc | ||
import pytest | ||
from utils import assert_column_eq | ||
|
||
|
||
@pytest.fixture() | ||
def str_data(): | ||
pa_data = pa.array(["A", None]) | ||
return pa_data, plc.interop.from_arrow(pa_data) | ||
|
||
|
||
def test_count_characters(str_data): | ||
result = plc.strings.attributes.count_characters(str_data[1]) | ||
expected = pc.utf8_length(str_data[0]) | ||
assert_column_eq(expected, result) | ||
|
||
|
||
def test_count_bytes(str_data): | ||
result = plc.strings.attributes.count_characters(str_data[1]) | ||
expected = pc.binary_length(str_data[0]) | ||
assert_column_eq(expected, result) | ||
|
||
|
||
def test_code_points(str_data): | ||
result = plc.strings.attributes.code_points(str_data[1]) | ||
exp_data = [ord(str_data[0].to_pylist()[0])] | ||
expected = pa.chunked_array([exp_data], type=pa.int32()) | ||
assert_column_eq(expected, result) |