Skip to content

Commit 03b2fb0

Browse files
jbrockmendelvladu
authored andcommitted
TYP: IndexOpsMixin (pandas-dev#40642)
1 parent 14c6dd5 commit 03b2fb0

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

pandas/core/base.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
DtypeObj,
2525
IndexLabel,
2626
Shape,
27+
final,
2728
)
2829
from pandas.compat import PYPY
2930
from pandas.compat.numpy import function as nv
@@ -933,12 +934,8 @@ def _map_values(self, mapper, na_action=None):
933934
# use the built in categorical series mapper which saves
934935
# time by mapping the categories instead of all values
935936

936-
# error: Incompatible types in assignment (expression has type
937-
# "Categorical", variable has type "IndexOpsMixin")
938-
self = cast("Categorical", self) # type: ignore[assignment]
939-
# error: Item "ExtensionArray" of "Union[ExtensionArray, Any]" has no
940-
# attribute "map"
941-
return self._values.map(mapper) # type: ignore[union-attr]
937+
cat = cast("Categorical", self._values)
938+
return cat.map(mapper)
942939

943940
values = self._values
944941

@@ -955,8 +952,7 @@ def _map_values(self, mapper, na_action=None):
955952
raise NotImplementedError
956953
map_f = lambda values, f: values.map(f)
957954
else:
958-
# error: "IndexOpsMixin" has no attribute "astype"
959-
values = self.astype(object)._values # type: ignore[attr-defined]
955+
values = self._values.astype(object)
960956
if na_action == "ignore":
961957
map_f = lambda values, f: lib.map_infer_mask(
962958
values, f, isna(values).view(np.uint8)
@@ -1327,9 +1323,10 @@ def searchsorted(self, value, side="left", sorter=None) -> np.ndarray:
13271323
return algorithms.searchsorted(self._values, value, side=side, sorter=sorter)
13281324

13291325
def drop_duplicates(self, keep="first"):
1330-
duplicated = self.duplicated(keep=keep)
1326+
duplicated = self._duplicated(keep=keep)
13311327
# error: Value of type "IndexOpsMixin" is not indexable
13321328
return self[~duplicated] # type: ignore[index]
13331329

1334-
def duplicated(self, keep: Union[str, bool] = "first") -> np.ndarray:
1330+
@final
1331+
def _duplicated(self, keep: Union[str, bool] = "first") -> np.ndarray:
13351332
return duplicated(self._values, keep=keep)

pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2663,7 +2663,7 @@ def duplicated(self, keep: Union[str_t, bool] = "first") -> np.ndarray:
26632663
26642664
Returns
26652665
-------
2666-
numpy.ndarray
2666+
np.ndarray[bool]
26672667
26682668
See Also
26692669
--------
@@ -2699,7 +2699,7 @@ def duplicated(self, keep: Union[str_t, bool] = "first") -> np.ndarray:
26992699
if self.is_unique:
27002700
# fastpath available bc we are immutable
27012701
return np.zeros(len(self), dtype=bool)
2702-
return super().duplicated(keep=keep)
2702+
return self._duplicated(keep=keep)
27032703

27042704
def _get_unique_index(self: _IndexT) -> _IndexT:
27052705
"""

pandas/core/indexes/multi.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1614,12 +1614,16 @@ def _inferred_type_levels(self) -> List[str]:
16141614
return [i.inferred_type for i in self.levels]
16151615

16161616
@doc(Index.duplicated)
1617-
def duplicated(self, keep="first"):
1617+
def duplicated(self, keep="first") -> np.ndarray:
16181618
shape = map(len, self.levels)
16191619
ids = get_group_index(self.codes, shape, sort=False, xnull=False)
16201620

16211621
return duplicated_int64(ids, keep)
16221622

1623+
# error: Cannot override final attribute "_duplicated"
1624+
# (previously declared in base class "IndexOpsMixin")
1625+
_duplicated = duplicated # type: ignore[misc]
1626+
16231627
def fillna(self, value=None, downcast=None):
16241628
"""
16251629
fillna is not implemented for MultiIndex
@@ -2216,11 +2220,7 @@ def drop(self, codes, level=None, errors="raise"):
22162220

22172221
if not isinstance(codes, (np.ndarray, Index)):
22182222
try:
2219-
# error: Argument "dtype" to "index_labels_to_array" has incompatible
2220-
# type "Type[object]"; expected "Union[str, dtype[Any], None]"
2221-
codes = com.index_labels_to_array(
2222-
codes, dtype=object # type: ignore[arg-type]
2223-
)
2223+
codes = com.index_labels_to_array(codes, dtype=np.dtype("object"))
22242224
except ValueError:
22252225
pass
22262226

pandas/core/series.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,7 @@ def mode(self, dropna=True) -> Series:
18741874
# TODO: Add option for bins like value_counts()
18751875
return algorithms.mode(self, dropna=dropna)
18761876

1877-
def unique(self):
1877+
def unique(self) -> ArrayLike:
18781878
"""
18791879
Return unique values of Series object.
18801880
@@ -2020,9 +2020,7 @@ def drop_duplicates(self, keep="first", inplace=False) -> Optional[Series]:
20202020
else:
20212021
return result
20222022

2023-
# error: Return type "Series" of "duplicated" incompatible with return type
2024-
# "ndarray" in supertype "IndexOpsMixin"
2025-
def duplicated(self, keep="first") -> Series: # type: ignore[override]
2023+
def duplicated(self, keep="first") -> Series:
20262024
"""
20272025
Indicate duplicate Series values.
20282026
@@ -2043,7 +2041,7 @@ def duplicated(self, keep="first") -> Series: # type: ignore[override]
20432041
20442042
Returns
20452043
-------
2046-
Series
2044+
Series[bool]
20472045
Series indicating whether each value has occurred in the
20482046
preceding values.
20492047
@@ -2098,7 +2096,7 @@ def duplicated(self, keep="first") -> Series: # type: ignore[override]
20982096
4 True
20992097
dtype: bool
21002098
"""
2101-
res = base.IndexOpsMixin.duplicated(self, keep=keep)
2099+
res = self._duplicated(keep=keep)
21022100
result = self._constructor(res, index=self.index)
21032101
return result.__finalize__(self, method="duplicated")
21042102

0 commit comments

Comments
 (0)