-
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.
Merge remote-tracking branch 'upstream/branch-24.12' into pylibcudf/s…
…trings/convert_lists
- Loading branch information
Showing
12 changed files
with
214 additions
and
50 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 |
---|---|---|
@@ -1,31 +1,19 @@ | ||
# Copyright (c) 2021-2024, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.utility cimport move | ||
|
||
from cudf.core.buffer import acquire_spill_lock | ||
|
||
from pylibcudf.libcudf.column.column cimport column | ||
from pylibcudf.libcudf.column.column_view cimport column_view | ||
from pylibcudf.libcudf.strings.convert.convert_floats cimport ( | ||
is_float as cpp_is_float, | ||
) | ||
|
||
from cudf._lib.column cimport Column | ||
|
||
import pylibcudf as plc | ||
|
||
|
||
@acquire_spill_lock() | ||
def is_float(Column source_strings): | ||
""" | ||
Returns a Column of boolean values with True for `source_strings` | ||
that have floats. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
cdef column_view source_view = source_strings.view() | ||
|
||
with nogil: | ||
c_result = move(cpp_is_float( | ||
source_view | ||
)) | ||
|
||
return Column.from_unique_ptr(move(c_result)) | ||
plc_column = plc.strings.convert.convert_floats.is_float( | ||
source_strings.to_pylibcudf(mode="read") | ||
) | ||
return Column.from_pylibcudf(plc_column) |
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
11 changes: 11 additions & 0 deletions
11
python/pylibcudf/pylibcudf/strings/convert/convert_floats.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_floats(Column strings, DataType output_type) | ||
|
||
cpdef Column from_floats(Column floats) | ||
|
||
cpdef Column is_float(Column input) |
101 changes: 101 additions & 0 deletions
101
python/pylibcudf/pylibcudf/strings/convert/convert_floats.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,101 @@ | ||
# 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_floats as cpp_convert_floats, | ||
) | ||
from pylibcudf.types cimport DataType | ||
|
||
|
||
cpdef Column to_floats(Column strings, DataType output_type): | ||
""" | ||
Returns a new numeric column by parsing float values from each string | ||
in the provided strings column. | ||
For details, see cpp:func:`cudf::strings::to_floats` | ||
Parameters | ||
---------- | ||
strings : Column | ||
Strings instance for this operation. | ||
output_type : DataType | ||
Type of float numeric column to return. | ||
Returns | ||
------- | ||
Column | ||
New column with floats converted from strings. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_convert_floats.to_floats( | ||
strings.view(), | ||
output_type.c_obj, | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column from_floats(Column floats): | ||
""" | ||
Returns a new strings column converting the float values from the | ||
provided column into strings. | ||
For details, see cpp:func:`cudf::strings::from_floats` | ||
Parameters | ||
---------- | ||
floats : Column | ||
Numeric column to convert. | ||
Returns | ||
------- | ||
Column | ||
New strings column with floats as strings. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_convert_floats.from_floats( | ||
floats.view(), | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column is_float(Column input): | ||
""" | ||
Returns a boolean column identifying strings in which all | ||
characters are valid for conversion to floats. | ||
For details, see cpp:func:`cudf::strings::is_float` | ||
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_floats.is_float( | ||
input.view(), | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) |
33 changes: 33 additions & 0 deletions
33
python/pylibcudf/pylibcudf/tests/test_string_convert_floats.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,33 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import pyarrow as pa | ||
import pylibcudf as plc | ||
from utils import assert_column_eq | ||
|
||
|
||
def test_to_floats(): | ||
typ = pa.float32() | ||
arr = pa.array(["-1.23", "1", None]) | ||
result = plc.strings.convert.convert_floats.to_floats( | ||
plc.interop.from_arrow(arr), plc.interop.from_arrow(typ) | ||
) | ||
expected = arr.cast(typ) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
def test_from_floats(): | ||
arr = pa.array([-1.23, 1, None]) | ||
result = plc.strings.convert.convert_floats.from_floats( | ||
plc.interop.from_arrow(arr), | ||
) | ||
expected = pa.array(["-1.23", "1.0", None]) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
def test_is_float(): | ||
arr = pa.array(["-1.23", "1", "1.2.3", "A", None]) | ||
result = plc.strings.convert.convert_floats.is_float( | ||
plc.interop.from_arrow(arr), | ||
) | ||
expected = pa.array([True, True, False, False, None]) | ||
assert_column_eq(result, expected) |