diff --git a/pandas/core/base.py b/pandas/core/base.py index f30430dd394ca..18fc76fe79a5a 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -24,6 +24,7 @@ DtypeObj, IndexLabel, Shape, + final, ) from pandas.compat import PYPY from pandas.compat.numpy import function as nv @@ -933,12 +934,8 @@ def _map_values(self, mapper, na_action=None): # use the built in categorical series mapper which saves # time by mapping the categories instead of all values - # error: Incompatible types in assignment (expression has type - # "Categorical", variable has type "IndexOpsMixin") - self = cast("Categorical", self) # type: ignore[assignment] - # error: Item "ExtensionArray" of "Union[ExtensionArray, Any]" has no - # attribute "map" - return self._values.map(mapper) # type: ignore[union-attr] + cat = cast("Categorical", self._values) + return cat.map(mapper) values = self._values @@ -955,8 +952,7 @@ def _map_values(self, mapper, na_action=None): raise NotImplementedError map_f = lambda values, f: values.map(f) else: - # error: "IndexOpsMixin" has no attribute "astype" - values = self.astype(object)._values # type: ignore[attr-defined] + values = self._values.astype(object) if na_action == "ignore": map_f = lambda values, f: lib.map_infer_mask( values, f, isna(values).view(np.uint8) @@ -1327,9 +1323,10 @@ def searchsorted(self, value, side="left", sorter=None) -> np.ndarray: return algorithms.searchsorted(self._values, value, side=side, sorter=sorter) def drop_duplicates(self, keep="first"): - duplicated = self.duplicated(keep=keep) + duplicated = self._duplicated(keep=keep) # error: Value of type "IndexOpsMixin" is not indexable return self[~duplicated] # type: ignore[index] - def duplicated(self, keep: Union[str, bool] = "first") -> np.ndarray: + @final + def _duplicated(self, keep: Union[str, bool] = "first") -> np.ndarray: return duplicated(self._values, keep=keep) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index af3315dd2ade6..fc4eeebc86642 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2663,7 +2663,7 @@ def duplicated(self, keep: Union[str_t, bool] = "first") -> np.ndarray: Returns ------- - numpy.ndarray + np.ndarray[bool] See Also -------- @@ -2699,7 +2699,7 @@ def duplicated(self, keep: Union[str_t, bool] = "first") -> np.ndarray: if self.is_unique: # fastpath available bc we are immutable return np.zeros(len(self), dtype=bool) - return super().duplicated(keep=keep) + return self._duplicated(keep=keep) def _get_unique_index(self: _IndexT) -> _IndexT: """ diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 76878d0a0b82a..fedb955ce83b9 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1614,12 +1614,16 @@ def _inferred_type_levels(self) -> List[str]: return [i.inferred_type for i in self.levels] @doc(Index.duplicated) - def duplicated(self, keep="first"): + def duplicated(self, keep="first") -> np.ndarray: shape = map(len, self.levels) ids = get_group_index(self.codes, shape, sort=False, xnull=False) return duplicated_int64(ids, keep) + # error: Cannot override final attribute "_duplicated" + # (previously declared in base class "IndexOpsMixin") + _duplicated = duplicated # type: ignore[misc] + def fillna(self, value=None, downcast=None): """ fillna is not implemented for MultiIndex @@ -2216,11 +2220,7 @@ def drop(self, codes, level=None, errors="raise"): if not isinstance(codes, (np.ndarray, Index)): try: - # error: Argument "dtype" to "index_labels_to_array" has incompatible - # type "Type[object]"; expected "Union[str, dtype[Any], None]" - codes = com.index_labels_to_array( - codes, dtype=object # type: ignore[arg-type] - ) + codes = com.index_labels_to_array(codes, dtype=np.dtype("object")) except ValueError: pass diff --git a/pandas/core/series.py b/pandas/core/series.py index 27042f7de9dc1..641a57a554a9b 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1874,7 +1874,7 @@ def mode(self, dropna=True) -> Series: # TODO: Add option for bins like value_counts() return algorithms.mode(self, dropna=dropna) - def unique(self): + def unique(self) -> ArrayLike: """ Return unique values of Series object. @@ -2020,9 +2020,7 @@ def drop_duplicates(self, keep="first", inplace=False) -> Optional[Series]: else: return result - # error: Return type "Series" of "duplicated" incompatible with return type - # "ndarray" in supertype "IndexOpsMixin" - def duplicated(self, keep="first") -> Series: # type: ignore[override] + def duplicated(self, keep="first") -> Series: """ Indicate duplicate Series values. @@ -2043,7 +2041,7 @@ def duplicated(self, keep="first") -> Series: # type: ignore[override] Returns ------- - Series + Series[bool] Series indicating whether each value has occurred in the preceding values. @@ -2098,7 +2096,7 @@ def duplicated(self, keep="first") -> Series: # type: ignore[override] 4 True dtype: bool """ - res = base.IndexOpsMixin.duplicated(self, keep=keep) + res = self._duplicated(keep=keep) result = self._constructor(res, index=self.index) return result.__finalize__(self, method="duplicated")