Skip to content
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

Migrate lists/filling to pylibcudf #16189

Merged
merged 6 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/libcudf/lists/filling.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2021-2024, NVIDIA CORPORATION.

from libcpp.memory cimport unique_ptr

from cudf._lib.pylibcudf.libcudf.column.column cimport column
from cudf._lib.pylibcudf.libcudf.column.column_view cimport column_view


cdef extern from "cudf/lists/filling.hpp" namespace "cudf::lists" nogil:
cdef unique_ptr[column] sequences(
const column_view& starts,
const column_view& sizes,
) except +

cdef unique_ptr[column] sequences(
const column_view& starts,
const column_view& steps,
const column_view& sizes,
) except +
2 changes: 2 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/lists.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ cpdef Column contains(Column, ColumnOrScalar)
cpdef Column contains_nulls(Column)

cpdef Column index_of(Column, ColumnOrScalar, bool)

cpdef Column sequences(Column, Column, Column steps = *)
40 changes: 40 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/lists.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from cudf._lib.pylibcudf.libcudf.column.column cimport column
from cudf._lib.pylibcudf.libcudf.lists cimport (
contains as cpp_contains,
explode as cpp_explode,
filling as cpp_filling,
)
from cudf._lib.pylibcudf.libcudf.lists.combine cimport (
concatenate_list_elements as cpp_concatenate_list_elements,
Expand Down Expand Up @@ -206,3 +207,42 @@ cpdef Column index_of(Column input, ColumnOrScalar search_key, bool find_first_o
find_option,
))
return Column.from_libcudf(move(c_result))


cpdef Column sequences(Column starts, Column sizes, Column steps = None):
"""Create a lists column in which each row contains a sequence of
values specified by a tuple of (start, step, size) parameters.


For details, see :cpp:func:`sequences`.

Parameters
----------
starts : Column
First values in the result sequences.
steps : Optional[Column]
vyasr marked this conversation as resolved.
Show resolved Hide resolved
Increment values for the result sequences.
sizes : Column
Numbers of values in the result sequences.

Returns
-------
Column
The result column containing generated sequences.
"""
cdef unique_ptr[column] c_result

if (steps):
vyasr marked this conversation as resolved.
Show resolved Hide resolved
with nogil:
c_result = move(cpp_filling.sequences(
starts.view(),
steps.view(),
sizes.view(),
))
else:
with nogil:
c_result = move(cpp_filling.sequences(
starts.view(),
sizes.view(),
))
return Column.from_libcudf(move(c_result))
16 changes: 16 additions & 0 deletions python/cudf/cudf/pylibcudf_tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,19 @@ def test_index_of_list_column(test_data, column):
expect = pa.array(column[1], type=pa.int32())

assert_column_eq(expect, res)


def test_sequences():
starts = plc.interop.from_arrow(pa.array([0, 1, 2, 3, 4]))
steps = plc.interop.from_arrow(pa.array([2, 1, 1, 1, -3]))
sizes = plc.interop.from_arrow(pa.array([0, 2, 2, 1, 3]))
Comment on lines +202 to +204
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't create fixtures because these columns will only be used in this test


res1 = plc.lists.sequences(starts, sizes, steps)
res2 = plc.lists.sequences(starts, sizes)

expect1 = pa.array([[], [1, 2], [2, 3], [3], [4, 1, -2]])
expect2 = pa.array([[], [1, 2], [2, 3], [3], [4, 5, 6]])

assert_column_eq(expect1, res1)

assert_column_eq(expect2, res2)
Loading