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 all 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
5 changes: 5 additions & 0 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,11 @@ def __cuda_array_interface__(self) -> abc.Mapping[str, Any]:
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
return _array_ufunc(self, ufunc, method, inputs, kwargs)

def __invert__(self):
raise TypeError(
f"Operation `~` not supported on {self.dtype.type.__name__}"
)

def searchsorted(
self,
value,
Expand Down
8 changes: 8 additions & 0 deletions python/cudf/cudf/core/column/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ def unary_operator(self, unaryop: Union[str, Callable]) -> ColumnBase:
unaryop = pylibcudf.unary.UnaryOperator[unaryop]
return libcudf.unary.unary_operation(self, unaryop)

def __invert__(self):
if self.dtype.kind in "ui":
return self.unary_operator("invert")
elif self.dtype.kind == "b":
return self.unary_operator("not")
else:
return super().__invert__()

def _binaryop(self, other: ColumnBinaryOperand, op: str) -> ColumnBase:
int_float_dtype_mapping = {
np.int8: np.float32,
Expand Down
61 changes: 2 additions & 59 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 @@ -1920,7 +1875,7 @@ def __invert__(self):
"""Bitwise invert (~) for integral dtypes, logical NOT for bools."""
return self._from_data_like_self(
self._data._from_columns_like_self(
(_apply_inverse_column(col) for col in self._data.columns)
(~col for col in self._data.columns)
)
)

Expand Down Expand Up @@ -1970,15 +1925,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