Skip to content

TYP: IndexOpsMixin #40642

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

Merged
merged 2 commits into from
Mar 30, 2021
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
17 changes: 7 additions & 10 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
DtypeObj,
IndexLabel,
Shape,
final,
)
from pandas.compat import PYPY
from pandas.compat.numpy import function as nv
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2663,7 +2663,7 @@ def duplicated(self, keep: Union[str_t, bool] = "first") -> np.ndarray:

Returns
-------
numpy.ndarray
np.ndarray[bool]

See Also
--------
Expand Down Expand Up @@ -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:
"""
Expand Down
12 changes: 6 additions & 6 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm this is not inherited?

Copy link
Member Author

Choose a reason for hiding this comment

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

it is inherited, and this is overriding the inherited method


def fillna(self, value=None, downcast=None):
"""
fillna is not implemented for MultiIndex
Expand Down Expand Up @@ -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

Expand Down
10 changes: 4 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand All @@ -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.

Expand Down Expand Up @@ -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")

Expand Down