Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt711 committed Jun 28, 2024
1 parent fb12d98 commit 800824c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
18 changes: 3 additions & 15 deletions python/cudf/cudf/_lib/lists.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ from libcpp.utility cimport move
from cudf._lib.column cimport Column
from cudf._lib.pylibcudf.libcudf.column.column cimport column
from cudf._lib.pylibcudf.libcudf.column.column_view cimport column_view
from cudf._lib.pylibcudf.libcudf.lists.count_elements cimport (
count_elements as cpp_count_elements,
)
from cudf._lib.pylibcudf.libcudf.lists.extract cimport extract_list_element
from cudf._lib.pylibcudf.libcudf.lists.lists_column_view cimport (
lists_column_view,
Expand All @@ -38,19 +35,10 @@ from cudf._lib.pylibcudf cimport Scalar

@acquire_spill_lock()
def count_elements(Column col):

# shared_ptr required because lists_column_view has no default
# ctor
cdef shared_ptr[lists_column_view] list_view = (
make_shared[lists_column_view](col.view())
return Column.from_pylibcudf(
pylibcudf.lists.count_elements(
col.to_pylibcudf(mode="read"))
)
cdef unique_ptr[column] c_result

with nogil:
c_result = move(cpp_count_elements(list_view.get()[0]))

result = Column.from_unique_ptr(move(c_result))
return result


@acquire_spill_lock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ from cudf._lib.pylibcudf.libcudf.lists.lists_column_view cimport (


cdef extern from "cudf/lists/count_elements.hpp" namespace "cudf::lists" nogil:
cdef unique_ptr[column] count_elements(const lists_column_view) except +
cdef unique_ptr[column] count_elements(const lists_column_view&) 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 count_elements(Column)
24 changes: 24 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/lists.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ from cudf._lib.pylibcudf.libcudf.lists.combine cimport (
concatenate_null_policy,
concatenate_rows as cpp_concatenate_rows,
)
from cudf._lib.pylibcudf.libcudf.lists.count_elements cimport (
count_elements as cpp_count_elements,
)
from cudf._lib.pylibcudf.libcudf.table.table cimport table
from cudf._lib.pylibcudf.libcudf.types cimport size_type
from cudf._lib.pylibcudf.lists cimport ColumnOrScalar
Expand Down Expand Up @@ -206,3 +209,24 @@ cpdef Column index_of(Column input, ColumnOrScalar search_key, bool find_first_o
find_option,
))
return Column.from_libcudf(move(c_result))


cpdef Column count_elements(Column input):
"""Count the number of rows in each
list element in the given lists column.
Parameters
----------
input : Column
The input column
Returns
-------
Column
A new Column of the lengths of each list element
"""
cdef ListColumnView list_view = input.list_view()
cdef unique_ptr[column] c_result

with nogil:
c_result = move(cpp_count_elements(list_view.view()))

return Column.from_libcudf(move(c_result))
10 changes: 10 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,13 @@ def test_index_of_list_column(test_data, column):
expect = pa.array(column[1], type=pa.int32())

assert_column_eq(expect, res)


def test_count_elements(test_data):
arr = pa.array(test_data[0][1])
plc_column = plc.interop.from_arrow(arr)
res = plc.lists.count_elements(plc_column)

expect = pa.array([1, 1, 0, 3], type=pa.int32())

assert_column_eq(expect, res)

0 comments on commit 800824c

Please sign in to comment.