Skip to content

Commit

Permalink
Move more methods into SingleColumnFrame (#8253)
Browse files Browse the repository at this point in the history
Continuation of #8115 and #8166. Moves more logic out of the Index/Series classes into the new common parent class to reduce code duplication and ensure feature parity.

Authors:
  - Vyas Ramasubramani (https://github.com/vyasr)

Approvers:
  - https://github.com/brandon-b-miller
  - Michael Wang (https://github.com/isVoid)

URL: #8253
  • Loading branch information
vyasr authored May 18, 2021
1 parent 414e9bb commit 8834ed6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 220 deletions.
79 changes: 78 additions & 1 deletion python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from cudf.utils.dtypes import (
is_categorical_dtype,
is_column_like,
is_numerical_dtype,
is_decimal_dtype,
is_numerical_dtype,
is_scalar,
min_scalar_type,
)
Expand Down Expand Up @@ -3364,6 +3364,10 @@ def __bool__(self):
"a.empty, a.bool(), a.item(), a.any() or a.all()."
)

@property
def _num_columns(self):
return 1

@property
def _column(self):
return self._data[self.name]
Expand Down Expand Up @@ -3529,6 +3533,79 @@ def to_arrow(self):
"""
return self._column.to_arrow()

@property
def is_unique(self):
"""Return boolean if values in the object are unique.
Returns
-------
bool
"""
return self._column.is_unique

@property
def is_monotonic(self):
"""Return boolean if values in the object are monotonic_increasing.
This property is an alias for :attr:`is_monotonic_increasing`.
Returns
-------
bool
"""
return self.is_monotonic_increasing

@property
def is_monotonic_increasing(self):
"""Return boolean if values in the object are monotonic_increasing.
Returns
-------
bool
"""
return self._column.is_monotonic_increasing

@property
def is_monotonic_decreasing(self):
"""Return boolean if values in the object are monotonic_decreasing.
Returns
-------
bool
"""
return self._column.is_monotonic_decreasing

@property
def __cuda_array_interface__(self):
return self._column.__cuda_array_interface__

def factorize(self, na_sentinel=-1):
"""Encode the input values as integer labels
Parameters
----------
na_sentinel : number
Value to indicate missing category.
Returns
--------
(labels, cats) : (cupy.ndarray, cupy.ndarray or Index)
- *labels* contains the encoded values
- *cats* contains the categories in order that the N-th
item corresponds to the (N-1) code.
Examples
--------
>>> import cudf
>>> s = cudf.Series(['a', 'a', 'c'])
>>> codes, uniques = s.factorize()
>>> codes
array([0, 0, 1], dtype=int8)
>>> uniques
StringIndex(['a' 'c'], dtype='object')
"""
return cudf.core.algorithms.factorize(self, na_sentinel=na_sentinel)

@property
def _copy_construct_defaults(self):
"""A default dictionary of kwargs to be used for copy construction."""
Expand Down
93 changes: 2 additions & 91 deletions python/cudf/cudf/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,6 @@ def _clean_nulls_from_index(self):
else:
return self

def factorize(self, na_sentinel=-1):
"""
Encode the input values as integer labels
See Also
--------
cudf.core.series.Series.factorize : Encode the input values of Series.
"""
return cudf.core.algorithms.factorize(self, na_sentinel=na_sentinel)

@property
def nlevels(self):
"""
Expand Down Expand Up @@ -1144,59 +1133,6 @@ def to_series(self, index=None, name=None):
name=self.name if name is None else name,
)

@property
def is_unique(self):
"""
Return if the index has unique values.
"""
raise (NotImplementedError)

@property
def is_monotonic(self):
"""
Alias for is_monotonic_increasing.
"""
return self.is_monotonic_increasing

@property
def is_monotonic_increasing(self):
"""
Return if the index is monotonic increasing
(only equal or increasing) values.
"""
return self._values.is_monotonic_increasing

@property
def is_monotonic_decreasing(self):
"""
Return if the index is monotonic decreasing
(only equal or decreasing) values.
"""
return self._values.is_monotonic_decreasing

@property
def empty(self):
"""
Indicator whether Index is empty.
True if Index is entirely empty (no items).
Returns
-------
out : bool
If Index is empty, return True, if not return False.
Examples
--------
>>> import cudf
>>> index = cudf.Index([])
>>> index
Float64Index([], dtype='float64')
>>> index.empty
True
"""
return not self.size

def get_slice_bound(self, label, side, kind):
"""
Calculate slice bound that corresponds to given label.
Expand Down Expand Up @@ -1278,9 +1214,7 @@ def isin(self, values):
array([ True, False, False])
"""

result = self.to_series().isin(values).values

return result
return self._values.isin(values).values

def where(self, cond, other=None):
"""
Expand Down Expand Up @@ -1312,10 +1246,6 @@ def where(self, cond, other=None):
"""
return super().where(cond=cond, other=other)

@property
def __cuda_array_interface__(self):
raise (NotImplementedError)

def memory_usage(self, deep=False):
"""
Memory usage of the values.
Expand Down Expand Up @@ -1508,10 +1438,6 @@ def step(self):
"""
return self._step

@property
def _num_columns(self):
return 1

@property
def _num_rows(self):
return len(self)
Expand Down Expand Up @@ -1773,10 +1699,6 @@ def get_slice_bound(self, label, side, kind=None):
pos = search_range(start, stop, label, step, side=side)
return pos

@property
def __cuda_array_interface__(self):
return self._values.__cuda_array_interface__

def memory_usage(self, **kwargs):
return 0

Expand Down Expand Up @@ -1838,7 +1760,7 @@ def _initialize(self, values, **kwargs):

@property
def _values(self):
return next(iter(self._data.columns))
return self._column

def copy(self, name=None, deep=False, dtype=None, names=None):
"""
Expand Down Expand Up @@ -1990,20 +1912,9 @@ def find_label_range(self, first, last):
end += 1
return begin, end

@property
def is_unique(self):
"""
Return if the index has unique values.
"""
return self._values.is_unique

def get_slice_bound(self, label, side, kind):
return self._values.get_slice_bound(label, side, kind)

@property
def __cuda_array_interface__(self):
return self._values.__cuda_array_interface__


class NumericIndex(GenericIndex):
"""Immutable, ordered and sliceable sequence of labels.
Expand Down
5 changes: 5 additions & 0 deletions python/cudf/cudf/core/multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ def names(self, value):
)
self._names = pd.core.indexes.frozen.FrozenList(value)

@property
def _num_columns(self):
# MultiIndex is not a single-columned frame.
return super(SingleColumnFrame, self)._num_columns

def rename(self, names, inplace=False):
"""
Alter MultiIndex level names
Expand Down
Loading

0 comments on commit 8834ed6

Please sign in to comment.