-
Notifications
You must be signed in to change notification settings - Fork 915
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
Cython API Refactor: transpose.pyx
, sort.pyx
#10675
Merged
rapids-bot
merged 8 commits into
rapidsai:branch-22.06
from
isVoid:refactor/list_of_columns/transpose_sort
Apr 19, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2d84400
Rewrites transpose cython API and update `dataframe.transpose`
isVoid fd2bb8e
Refactor `is_sorted`
isVoid db9ac6e
Refactor `order_by`
isVoid 46ab812
Rename sort.pyx variables
isVoid 1742805
Refactor `digitize`
isVoid 6a765c9
Reduce digitize tests counts, move to test_series.py
isVoid 2993fbc
Refactor `rank`
isVoid e2f7c27
Move rank to `indexed_frame`
isVoid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,73 +1,27 @@ | ||
# Copyright (c) 2020, NVIDIA CORPORATION. | ||
|
||
import cudf | ||
from cudf.api.types import is_categorical_dtype | ||
# Copyright (c) 2020-2022, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.pair cimport pair | ||
from libcpp.utility cimport move | ||
|
||
from cudf._lib.column cimport Column | ||
from cudf._lib.cpp.column.column cimport column | ||
from cudf._lib.cpp.column.column_view cimport column_view | ||
from cudf._lib.cpp.table.table cimport table | ||
from cudf._lib.cpp.table.table_view cimport table_view | ||
from cudf._lib.cpp.transpose cimport transpose as cpp_transpose | ||
from cudf._lib.utils cimport data_from_table_view, table_view_from_table | ||
|
||
from cudf._lib.utils cimport columns_from_table_view, table_view_from_columns | ||
|
||
def transpose(source): | ||
"""Transpose index and columns. | ||
|
||
See Also | ||
-------- | ||
cudf.core.DataFrame.transpose | ||
def transpose(list source_columns): | ||
"""Transpose m n-row columns into n m-row columns | ||
""" | ||
|
||
if source._num_columns == 0: | ||
return source | ||
|
||
cats = None | ||
columns = source._columns | ||
dtype = columns[0].dtype | ||
|
||
if is_categorical_dtype(dtype): | ||
if any(not is_categorical_dtype(c.dtype) for c in columns): | ||
raise ValueError('Columns must all have the same dtype') | ||
cats = list(c.categories for c in columns) | ||
cats = cudf.core.column.concat_columns(cats).unique() | ||
source = cudf.core.frame.Frame(index=source._index, data=[ | ||
(name, col._set_categories(cats, is_unique=True).codes) | ||
for name, col in source._data.items() | ||
]) | ||
elif any(c.dtype != dtype for c in columns): | ||
raise ValueError('Columns must all have the same dtype') | ||
|
||
cdef pair[unique_ptr[column], table_view] c_result | ||
cdef table_view c_input = table_view_from_table( | ||
source, ignore_index=True) | ||
cdef table_view c_input = table_view_from_columns(source_columns) | ||
|
||
with nogil: | ||
c_result = move(cpp_transpose(c_input)) | ||
|
||
result_owner = Column.from_unique_ptr(move(c_result.first)) | ||
data, _ = data_from_table_view( | ||
return columns_from_table_view( | ||
c_result.second, | ||
owner=result_owner, | ||
column_names=range(c_input.num_rows()) | ||
owners=[result_owner] * c_result.second.num_columns() | ||
) | ||
|
||
if cats is not None: | ||
data= [ | ||
(name, cudf.core.column.column.build_categorical_column( | ||
codes=cudf.core.column.column.build_column( | ||
col.base_data, dtype=col.dtype), | ||
mask=col.base_mask, | ||
size=col.size, | ||
categories=cats, | ||
offset=col.offset, | ||
)) | ||
for name, col in data.items() | ||
] | ||
|
||
return data |
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 |
---|---|---|
|
@@ -317,10 +317,10 @@ cdef columns_from_table_view( | |
): | ||
""" | ||
Given a ``cudf::table_view``, construsts a list of columns from it, | ||
along with referencing an ``owner`` Python object that owns the memory | ||
lifetime. ``owner`` must be either None or a list of column. If ``owner`` | ||
is a list of columns, the owner of the `i`th ``cudf::column_view`` in the | ||
table view is ``owners[i]``. For more about memory ownership, | ||
along with referencing an owner Python object that owns the memory | ||
lifetime. owner must be either None or a list of column. If owner | ||
is a list of columns, the owner of the `i`th ``cudf::column_view`` | ||
in the table view is ``owners[i]``. For more about memory ownership, | ||
Comment on lines
+320
to
+323
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current docstring is refers to |
||
see ``Column.from_column_view``. | ||
""" | ||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change to
transpose
converts the categorical column into numerical column codes. These calls depends on higher level APIs/external APIs, which I would like to avoid in cython. I thus moved them to the python API.