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

Move some misc Frame methods to appropriate locations #15963

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
70 changes: 12 additions & 58 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import cudf
from cudf import _lib as libcudf
from cudf._typing import Dtype
from cudf.api.types import is_bool_dtype, is_dtype_equal, is_scalar
from cudf.api.types import is_dtype_equal, is_scalar
from cudf.core.buffer import acquire_spill_lock
from cudf.core.column import (
ColumnBase,
Expand Down Expand Up @@ -1455,51 +1455,6 @@ def _get_sorted_inds(
stable=True,
)

@_cudf_nvtx_annotate
def _is_sorted(self, ascending=None, null_position=None):
"""
Returns a boolean indicating whether the data of the Frame are sorted
based on the parameters given. Does not account for the index.

Parameters
----------
self : Frame
Frame whose columns are to be checked for sort order
ascending : None or list-like of booleans
None or list-like of boolean values indicating expected sort order
of each column. If list-like, size of list-like must be
len(columns). If None, all columns expected sort order is set to
ascending. False (0) - ascending, True (1) - descending.
null_position : None or list-like of booleans
None or list-like of boolean values indicating desired order of
nulls compared to other elements. If list-like, size of list-like
must be len(columns). If None, null order is set to before. False
(0) - before, True (1) - after.

Returns
-------
returns : boolean
Returns True, if sorted as expected by ``ascending`` and
``null_position``, False otherwise.
"""
if ascending is not None and not cudf.api.types.is_list_like(
ascending
):
raise TypeError(
f"Expected a list-like or None for `ascending`, got "
f"{type(ascending)}"
)
if null_position is not None and not cudf.api.types.is_list_like(
null_position
):
raise TypeError(
f"Expected a list-like or None for `null_position`, got "
f"{type(null_position)}"
)
return libcudf.sort.is_sorted(
[*self._columns], ascending=ascending, null_position=null_position
)

@_cudf_nvtx_annotate
def _split(self, splits):
"""Split a frame with split points in ``splits``. Returns a list of
Expand Down Expand Up @@ -1918,6 +1873,17 @@ def __copy__(self):
@_cudf_nvtx_annotate
def __invert__(self):
"""Bitwise invert (~) for integral dtypes, logical NOT for bools."""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we define _apply_inverse_column outside of this __invert__ separately? That way we will not keep re-defining this method when __invert__ is invoked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You gave me the idea to define __invert__ on the Column so we can avoid this helper all together

def _apply_inverse_column(col: ColumnBase) -> ColumnBase:
if col.dtype.kind in "ui":
return col.unary_operator("invert")
elif col.dtype.kind == "b":
return col.unary_operator("not")
else:
raise TypeError(
f"Operation `~` not supported on {col.dtype.type.__name__}"
)

return self._from_data_like_self(
self._data._from_columns_like_self(
(_apply_inverse_column(col) for col in self._data.columns)
Expand Down Expand Up @@ -1970,15 +1936,3 @@ def __dask_tokenize__(self):
str(dict(self._dtypes)),
normalize_token(self.to_pandas()),
]


def _apply_inverse_column(col: ColumnBase) -> ColumnBase:
"""Bitwise invert (~) for integral dtypes, logical NOT for bools."""
if np.issubdtype(col.dtype, np.integer):
return col.unary_operator("invert")
elif is_bool_dtype(col.dtype):
return col.unary_operator("not")
else:
raise TypeError(
f"Operation `~` not supported on {col.dtype.type.__name__}"
)
49 changes: 47 additions & 2 deletions python/cudf/cudf/core/multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1636,9 +1636,54 @@ def is_unique(self):
def dtype(self):
return np.dtype("O")

@_cudf_nvtx_annotate
def _is_sorted(self, ascending=None, null_position=None) -> bool:
"""
Returns a boolean indicating whether the data of the MultiIndex are sorted
based on the parameters given. Does not account for the index.

Parameters
----------
self : MultiIndex
MultiIndex whose columns are to be checked for sort order
ascending : None or list-like of booleans
None or list-like of boolean values indicating expected sort order
of each column. If list-like, size of list-like must be
len(columns). If None, all columns expected sort order is set to
ascending. False (0) - ascending, True (1) - descending.
null_position : None or list-like of booleans
None or list-like of boolean values indicating desired order of
nulls compared to other elements. If list-like, size of list-like
must be len(columns). If None, null order is set to before. False
(0) - before, True (1) - after.

Returns
-------
returns : boolean
Returns True, if sorted as expected by ``ascending`` and
``null_position``, False otherwise.
"""
if ascending is not None and not cudf.api.types.is_list_like(
ascending
):
raise TypeError(
f"Expected a list-like or None for `ascending`, got "
f"{type(ascending)}"
)
if null_position is not None and not cudf.api.types.is_list_like(
null_position
):
raise TypeError(
f"Expected a list-like or None for `null_position`, got "
f"{type(null_position)}"
)
return libcudf.sort.is_sorted(
[*self._columns], ascending=ascending, null_position=null_position
)

@cached_property # type: ignore
@_cudf_nvtx_annotate
def is_monotonic_increasing(self):
def is_monotonic_increasing(self) -> bool:
"""
Return if the index is monotonic increasing
(only equal or increasing) values.
Expand All @@ -1647,7 +1692,7 @@ def is_monotonic_increasing(self):

@cached_property # type: ignore
@_cudf_nvtx_annotate
def is_monotonic_decreasing(self):
def is_monotonic_decreasing(self) -> bool:
"""
Return if the index is monotonic decreasing
(only equal or decreasing) values.
Expand Down
Loading