Skip to content

Commit

Permalink
Migrate lists/modifying to pylibcudf (#16185)
Browse files Browse the repository at this point in the history
Apart of #15162

Authors:
  - Matthew Murray (https://github.com/Matt711)
  - Lawrence Mitchell (https://github.com/wence-)

Approvers:
  - Lawrence Mitchell (https://github.com/wence-)

URL: #16185
  • Loading branch information
Matt711 authored Jul 4, 2024
1 parent c1c62f1 commit f3a1216
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
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) 2024, NVIDIA CORPORATION.

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)

0 comments on commit f3a1216

Please sign in to comment.