-
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.wrap APIs to pylibcudf #16935
Merged
rapids-bot
merged 5 commits into
rapidsai:branch-24.12
from
mroeschke:pylibcudf/strings/wrap
Oct 2, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9dcd067
Add string.wrap APIs to pylibcudf
mroeschke 695a46e
Merge branch 'branch-24.12' into pylibcudf/strings/wrap
vyasr 727edae
Merge branch 'branch-24.12' into pylibcudf/strings/wrap
mroeschke 4ce4f6a
Merge branch 'branch-24.12' into pylibcudf/strings/wrap
mroeschke 2bbf84c
Merge branch 'branch-24.12' into pylibcudf/strings/wrap
mroeschke 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ strings | |
replace | ||
slice | ||
strip | ||
wrap |
6 changes: 6 additions & 0 deletions
6
docs/cudf/source/user_guide/api_docs/pylibcudf/strings/wrap.rst
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,6 @@ | ||
==== | ||
wrap | ||
==== | ||
|
||
.. automodule:: pylibcudf.strings.wrap | ||
:members: |
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 |
---|---|---|
|
@@ -29,6 +29,7 @@ set(cython_sources | |
slice.pyx | ||
strip.pyx | ||
translate.pyx | ||
wrap.pyx | ||
) | ||
|
||
set(linked_libraries cudf::cudf) | ||
|
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,7 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from pylibcudf.column cimport Column | ||
from pylibcudf.libcudf.types cimport size_type | ||
|
||
|
||
cpdef Column wrap(Column input, size_type width) |
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,42 @@ | ||
# 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 wrap as cpp_wrap | ||
from pylibcudf.libcudf.types cimport size_type | ||
|
||
|
||
cpdef Column wrap(Column input, size_type width): | ||
""" | ||
Wraps strings onto multiple lines shorter than `width` by | ||
replacing appropriate white space with | ||
new-line characters (ASCII 0x0A). | ||
|
||
For details, see :cpp:func:`cudf::strings::wrap`. | ||
|
||
Parameters | ||
---------- | ||
input : Column | ||
String column | ||
|
||
width : int | ||
Maximum character width of a line within each string | ||
|
||
Returns | ||
------- | ||
Column | ||
Column of wrapped strings | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_wrap.wrap( | ||
input.view(), | ||
width, | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) |
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,24 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
import textwrap | ||
|
||
import pyarrow as pa | ||
import pylibcudf as plc | ||
from utils import assert_column_eq | ||
|
||
|
||
def test_wrap(): | ||
pa_array = pa.array( | ||
[ | ||
"the quick brown fox jumped over the lazy brown dog", | ||
"hello, world", | ||
None, | ||
] | ||
) | ||
result = plc.strings.wrap.wrap(plc.interop.from_arrow(pa_array), 12) | ||
expected = pa.array( | ||
[ | ||
textwrap.fill(val, 12) if isinstance(val, str) else val | ||
for val in pa_array.to_pylist() | ||
] | ||
) | ||
assert_column_eq(expected, result) |
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.
Minor nit: saving 12 as a variable would be better than hardcoding it twice.
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.
Will include this change in #16971