Skip to content

REF: IndexOpsMixin wrapping #36848

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 7 commits into from
Oct 8, 2020
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
39 changes: 19 additions & 20 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import builtins
import textwrap
from typing import Any, Callable, Dict, FrozenSet, List, Optional, Union, cast
from typing import Any, Callable, Dict, FrozenSet, List, Optional, TypeVar, Union, cast

import numpy as np

Expand Down Expand Up @@ -43,6 +43,8 @@
duplicated="IndexOpsMixin",
)

_T = TypeVar("_T", bound="IndexOpsMixin")


class PandasObject(DirNamesMixin):
"""
Expand Down Expand Up @@ -604,7 +606,7 @@ def _values(self) -> Union[ExtensionArray, np.ndarray]:
# must be defined here as a property for mypy
raise AbstractMethodError(self)

def transpose(self, *args, **kwargs):
def transpose(self: _T, *args, **kwargs) -> _T:
"""
Return the transpose, which is by definition self.

Expand Down Expand Up @@ -851,10 +853,10 @@ def to_numpy(self, dtype=None, copy=False, na_value=lib.no_default, **kwargs):
return result

@property
def empty(self):
def empty(self) -> bool:
return not self.size

def max(self, axis=None, skipna=True, *args, **kwargs):
def max(self, axis=None, skipna: bool = True, *args, **kwargs):
"""
Return the maximum value of the Index.

Expand Down Expand Up @@ -899,7 +901,7 @@ def max(self, axis=None, skipna=True, *args, **kwargs):
return nanops.nanmax(self._values, skipna=skipna)

@doc(op="max", oppose="min", value="largest")
def argmax(self, axis=None, skipna=True, *args, **kwargs):
def argmax(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
"""
Return int position of the {value} value in the Series.

Expand Down Expand Up @@ -954,7 +956,7 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs):
nv.validate_argmax_with_skipna(skipna, args, kwargs)
return nanops.nanargmax(self._values, skipna=skipna)

def min(self, axis=None, skipna=True, *args, **kwargs):
def min(self, axis=None, skipna: bool = True, *args, **kwargs):
"""
Return the minimum value of the Index.

Expand Down Expand Up @@ -999,7 +1001,7 @@ def min(self, axis=None, skipna=True, *args, **kwargs):
return nanops.nanmin(self._values, skipna=skipna)

@doc(argmax, op="min", oppose="max", value="smallest")
def argmin(self, axis=None, skipna=True, *args, **kwargs):
def argmin(self, axis=None, skipna=True, *args, **kwargs) -> int:
nv.validate_minmax_axis(axis)
nv.validate_argmax_with_skipna(skipna, args, kwargs)
return nanops.nanargmin(self._values, skipna=skipna)
Expand Down Expand Up @@ -1054,6 +1056,9 @@ def hasnans(self):
"""
return bool(isna(self).any())

def isna(self):
return isna(self._values)

def _reduce(
self,
op,
Expand Down Expand Up @@ -1161,7 +1166,12 @@ def map_f(values, f):
return new_values

def value_counts(
self, normalize=False, sort=True, ascending=False, bins=None, dropna=True
self,
normalize: bool = False,
sort: bool = True,
ascending: bool = False,
bins=None,
dropna: bool = True,
):
"""
Return a Series containing counts of unique values.
Expand Down Expand Up @@ -1500,20 +1510,9 @@ 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"):
if isinstance(self, ABCIndexClass):
if self.is_unique:
return self._shallow_copy()

duplicated = self.duplicated(keep=keep)
result = self[np.logical_not(duplicated)]
return result

def duplicated(self, keep="first"):
if isinstance(self, ABCIndexClass):
if self.is_unique:
return np.zeros(len(self), dtype=bool)
return duplicated(self, keep=keep)
else:
return self._constructor(
duplicated(self, keep=keep), index=self.index
).__finalize__(self, method="duplicated")
return duplicated(self._values, keep=keep)
6 changes: 6 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,9 @@ def drop_duplicates(self, keep="first"):
>>> idx.drop_duplicates(keep=False)
Index(['cow', 'beetle', 'hippo'], dtype='object')
"""
if self.is_unique:
return self._shallow_copy()

return super().drop_duplicates(keep=keep)

def duplicated(self, keep="first"):
Expand Down Expand Up @@ -2422,6 +2425,9 @@ def duplicated(self, keep="first"):
>>> idx.duplicated(keep=False)
array([ True, False, True, False, True])
"""
if self.is_unique:
# fastpath available bc we are immutable
return np.zeros(len(self), dtype=bool)
return super().duplicated(keep=keep)

def _get_unique_index(self, dropna: bool = False):
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,9 @@ def duplicated(self, keep="first") -> "Series":
4 True
dtype: bool
"""
return super().duplicated(keep=keep)
res = base.IndexOpsMixin.duplicated(self, keep=keep)
result = self._constructor(res, index=self.index)
return result.__finalize__(self, method="duplicated")

def idxmin(self, axis=0, skipna=True, *args, **kwargs):
"""
Expand Down Expand Up @@ -4776,7 +4778,7 @@ def _convert_dtypes(

@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"])
def isna(self) -> "Series":
return super().isna()
return generic.NDFrame.isna(self)

@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"])
def isnull(self) -> "Series":
Expand Down