-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add string.convert.convert_ipv4 APIs to pylibcudf (#16994)
Contributes to #15162 Authors: - Matthew Roeschke (https://github.com/mroeschke) Approvers: - Vyas Ramasubramani (https://github.com/vyasr) URL: #16994
- Loading branch information
Showing
8 changed files
with
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ from . cimport ( | |
convert_datetime, | ||
convert_durations, | ||
convert_fixed_point, | ||
convert_ipv4, | ||
) |
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
convert_datetime, | ||
convert_durations, | ||
convert_fixed_point, | ||
convert_ipv4, | ||
) |
10 changes: 10 additions & 0 deletions
10
python/pylibcudf/pylibcudf/strings/convert/convert_ipv4.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,10 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from pylibcudf.column cimport Column | ||
|
||
|
||
cpdef Column ipv4_to_integers(Column input) | ||
|
||
cpdef Column integers_to_ipv4(Column integers) | ||
|
||
cpdef Column is_ipv4(Column input) |
92 changes: 92 additions & 0 deletions
92
python/pylibcudf/pylibcudf/strings/convert/convert_ipv4.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,92 @@ | ||
# 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_ipv4 as cpp_convert_ipv4 | ||
|
||
|
||
cpdef Column ipv4_to_integers(Column input): | ||
""" | ||
Converts IPv4 addresses into integers. | ||
For details, see cpp:func:`cudf::strings::ipv4_to_integers` | ||
Parameters | ||
---------- | ||
input : Column | ||
Strings instance for this operation | ||
Returns | ||
------- | ||
Column | ||
New uint32 column converted from strings. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_convert_ipv4.ipv4_to_integers( | ||
input.view() | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column integers_to_ipv4(Column integers): | ||
""" | ||
Converts integers into IPv4 addresses as strings. | ||
For details, see cpp:func:`cudf::strings::integers_to_ipv4` | ||
Parameters | ||
---------- | ||
integers : Column | ||
Integer (uint32) column to convert. | ||
Returns | ||
------- | ||
Column | ||
New strings column. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_convert_ipv4.integers_to_ipv4( | ||
integers.view() | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column is_ipv4(Column input): | ||
""" | ||
Returns a boolean column identifying strings in which all | ||
characters are valid for conversion to integers from IPv4 format. | ||
For details, see cpp:func:`cudf::strings::is_ipv4` | ||
Parameters | ||
---------- | ||
input : Column | ||
Strings instance for this operation. | ||
Returns | ||
------- | ||
Column | ||
New column of boolean results for each string. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_convert_ipv4.is_ipv4( | ||
input.view() | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) |
31 changes: 31 additions & 0 deletions
31
python/pylibcudf/pylibcudf/tests/test_string_convert_ipv4.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,31 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
import pyarrow as pa | ||
import pylibcudf as plc | ||
from utils import assert_column_eq | ||
|
||
|
||
def test_ipv4_to_integers(): | ||
arr = pa.array(["123.45.67.890", None]) | ||
result = plc.strings.convert.convert_ipv4.ipv4_to_integers( | ||
plc.interop.from_arrow(arr) | ||
) | ||
expected = pa.array([2066564730, None], type=pa.uint32()) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
def test_integers_to_ipv4(): | ||
arr = pa.array([1, 0, None], type=pa.uint32()) | ||
result = plc.strings.convert.convert_ipv4.integers_to_ipv4( | ||
plc.interop.from_arrow(arr) | ||
) | ||
expected = pa.array(["0.0.0.1", "0.0.0.0", None]) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
def test_is_ipv4(): | ||
arr = pa.array(["0.0.0.1", "1.2.34", "A", None]) | ||
result = plc.strings.convert.convert_ipv4.is_ipv4( | ||
plc.interop.from_arrow(arr) | ||
) | ||
expected = pa.array([True, False, False, None]) | ||
assert_column_eq(result, expected) |