-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python API for
LIstMethods.len()
(#7283)
Closes #7157 This PR adds `ListMethods.len()` API that returns an integer column that contains the length for each element in a `ListColumn`. Example: ```python >>> s = cudf.Series([[1,2], None, [3]]) >>> s 0 [1, 2] 1 None 2 [3] dtype: list >>> s.list.len() 0 2 1 <NA> 2 1 dtype: int32 ``` Authors: - Michael Wang (@isVoid) - Ashwin Srinath (@shwina) Approvers: - Keith Kraus (@kkraus14) - @brandon-b-miller URL: #7283
- Loading branch information
Showing
5 changed files
with
91 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright (c) 2021, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
|
||
from cudf._lib.cpp.column.column cimport column | ||
from cudf._lib.cpp.lists.lists_column_view cimport lists_column_view | ||
|
||
cdef extern from "cudf/lists/count_elements.hpp" namespace "cudf::lists" nogil: | ||
cdef unique_ptr[column] count_elements(const lists_column_view) except + |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright (c) 2021, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr, shared_ptr, make_shared | ||
from libcpp.utility cimport move | ||
|
||
from cudf._lib.cpp.lists.count_elements cimport ( | ||
count_elements as cpp_count_elements | ||
) | ||
from cudf._lib.cpp.lists.lists_column_view cimport lists_column_view | ||
from cudf._lib.cpp.column.column_view cimport column_view | ||
from cudf._lib.cpp.column.column cimport column | ||
|
||
from cudf._lib.column cimport Column | ||
|
||
|
||
from cudf.core.dtypes import ListDtype | ||
|
||
|
||
def count_elements(Column col): | ||
if not isinstance(col.dtype, ListDtype): | ||
raise TypeError("col is not a list column.") | ||
|
||
# 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()) | ||
) | ||
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 |
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