diff --git a/python/cudf/cudf/core/_base_index.py b/python/cudf/cudf/core/_base_index.py index e6868ae3431..baca7b19e58 100644 --- a/python/cudf/cudf/core/_base_index.py +++ b/python/cudf/cudf/core/_base_index.py @@ -149,7 +149,7 @@ def ndim(self) -> int: # noqa: D401 """Number of dimensions of the underlying data, by definition 1.""" return 1 - def equals(self, other): + def equals(self, other) -> bool: """ Determine if two Index objects contain the same elements. diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index acfc2d781a7..0fc36fa80e4 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -2590,7 +2590,7 @@ def items(self): yield (k, self[k]) @_cudf_nvtx_annotate - def equals(self, other): + def equals(self, other) -> bool: ret = super().equals(other) # If all other checks matched, validate names. if ret: diff --git a/python/cudf/cudf/core/frame.py b/python/cudf/cudf/core/frame.py index d60c206ac24..7326696c994 100644 --- a/python/cudf/cudf/core/frame.py +++ b/python/cudf/cudf/core/frame.py @@ -273,20 +273,13 @@ def __len__(self) -> int: return self._num_rows @_cudf_nvtx_annotate - def astype(self, dtype, copy: bool = False): - result_data = { - col_name: col.astype(dtype.get(col_name, col.dtype), copy=copy) + def astype(self, dtype: dict[Any, Dtype], copy: bool = False) -> Self: + casted = ( + col.astype(dtype.get(col_name, col.dtype), copy=copy) for col_name, col in self._data.items() - } - - return ColumnAccessor( - data=result_data, - multiindex=self._data.multiindex, - level_names=self._data.level_names, - rangeindex=self._data.rangeindex, - label_dtype=self._data.label_dtype, - verify=False, ) + ca = self._data._from_columns_like_self(casted, verify=False) + return self._from_data_like_self(ca) @_cudf_nvtx_annotate def equals(self, other) -> bool: @@ -349,11 +342,7 @@ def equals(self, other) -> bool: """ if self is other: return True - if ( - other is None - or not isinstance(other, type(self)) - or len(self) != len(other) - ): + if not isinstance(other, type(self)) or len(self) != len(other): return False return all( diff --git a/python/cudf/cudf/core/index.py b/python/cudf/cudf/core/index.py index 2a75b374a1e..9b4c5473438 100644 --- a/python/cudf/cudf/core/index.py +++ b/python/cudf/cudf/core/index.py @@ -445,7 +445,7 @@ def __getitem__(self, index): return self._as_int_index()[index] @_cudf_nvtx_annotate - def equals(self, other): + def equals(self, other) -> bool: if isinstance(other, RangeIndex): return self._range == other._range return self._as_int_index().equals(other) @@ -1058,6 +1058,16 @@ def _from_data(cls, data: MutableMapping, name: Any = no_default) -> Self: out.name = name return out + @classmethod + @_cudf_nvtx_annotate + def _from_data_like_self( + cls, data: MutableMapping, name: Any = no_default + ) -> Self: + out = _index_from_data(data, name) + if name is not no_default: + out.name = name + return out + @classmethod @_cudf_nvtx_annotate def from_arrow(cls, obj): @@ -1180,12 +1190,8 @@ def is_unique(self): return self._column.is_unique @_cudf_nvtx_annotate - def equals(self, other): - if ( - other is None - or not isinstance(other, BaseIndex) - or len(self) != len(other) - ): + def equals(self, other) -> bool: + if not isinstance(other, BaseIndex) or len(self) != len(other): return False check_dtypes = False @@ -1231,7 +1237,7 @@ def copy(self, name=None, deep=False): @_cudf_nvtx_annotate def astype(self, dtype, copy: bool = True): - return _index_from_data(super().astype({self.name: dtype}, copy)) + return super().astype({self.name: dtype}, copy) @_cudf_nvtx_annotate def get_indexer(self, target, method=None, limit=None, tolerance=None): diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index a31430e1571..5a466f20f8c 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -625,10 +625,8 @@ def copy(self, deep: bool = True) -> Self: ) @_cudf_nvtx_annotate - def equals(self, other): # noqa: D102 - if not super().equals(other): - return False - return self.index.equals(other.index) + def equals(self, other) -> bool: # noqa: D102 + return super().equals(other) and self.index.equals(other.index) @property def index(self): @@ -4896,10 +4894,10 @@ def repeat(self, repeats, axis=None): def astype( self, - dtype, + dtype: dict[Any, Dtype], copy: bool = False, errors: Literal["raise", "ignore"] = "raise", - ): + ) -> Self: """Cast the object to the given dtype. Parameters @@ -5010,14 +5008,12 @@ def astype( raise ValueError("invalid error value specified") try: - data = super().astype(dtype, copy) + return super().astype(dtype, copy) except Exception as e: if errors == "raise": raise e return self - return self._from_data(data, index=self.index) - @_cudf_nvtx_annotate def drop( self,