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/modifying to pylibcudf #16185

Merged
merged 2 commits into from
Jul 4, 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
14 changes: 14 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/libcudf/lists/reverse.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
wence- marked this conversation as resolved.
Show resolved Hide resolved

from libcpp.memory cimport unique_ptr

from cudf._lib.pylibcudf.libcudf.column.column cimport column
from cudf._lib.pylibcudf.libcudf.lists.lists_column_view cimport (
lists_column_view,
)


cdef extern from "cudf/lists/reverse.hpp" namespace "cudf::lists" nogil:
cdef unique_ptr[column] reverse(
const lists_column_view& lists_column,
) 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 reverse(Column)
26 changes: 26 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,
reverse as cpp_reverse,
)
from cudf._lib.pylibcudf.libcudf.lists.combine cimport (
concatenate_list_elements as cpp_concatenate_list_elements,
Expand Down Expand Up @@ -206,3 +207,28 @@ cpdef Column index_of(Column input, ColumnOrScalar search_key, bool find_first_o
find_option,
))
return Column.from_libcudf(move(c_result))


cpdef Column reverse(Column input):
"""Reverse the element order within each list of the input column.

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

Parameters
----------
input : Column
The input column.

Returns
-------
Column
A new Column with reversed lists.
"""
cdef unique_ptr[column] c_result
cdef ListColumnView list_view = input.list_view()

with nogil:
c_result = move(cpp_reverse.reverse(
list_view.view(),
))
return Column.from_libcudf(move(c_result))
12 changes: 12 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,15 @@ def test_index_of_list_column(test_data, column):
expect = pa.array(column[1], type=pa.int32())

assert_column_eq(expect, res)


def test_reverse(test_data):
list_column = test_data[0][0]
arr = pa.array(list_column)
plc_column = plc.interop.from_arrow(arr)

res = plc.lists.reverse(plc_column)

expect = pa.array([lst[::-1] for lst in list_column])

assert_column_eq(expect, res)
Loading