Skip to content

Commit d5bfd2b

Browse files
authored
REF: move methods from base to Index (#52447)
* REF: move from base to Index * lint fixups
1 parent 9722fc9 commit d5bfd2b

File tree

3 files changed

+91
-128
lines changed

3 files changed

+91
-128
lines changed

pandas/core/base.py

+2-114
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
from pandas._libs import lib
2525
from pandas._typing import (
26-
Axis,
2726
AxisInt,
2827
DtypeObj,
2928
IndexLabel,
@@ -588,7 +587,8 @@ def to_numpy(
588587
values = np.asarray(values, dtype=dtype)
589588
else:
590589
values = values.copy()
591-
values[np.asanyarray(self.isna())] = na_value
590+
591+
values[np.asanyarray(isna(self))] = na_value
592592

593593
result = np.asarray(values, dtype=dtype)
594594

@@ -608,50 +608,6 @@ def to_numpy(
608608
def empty(self) -> bool:
609609
return not self.size
610610

611-
def max(self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs):
612-
"""
613-
Return the maximum value of the Index.
614-
615-
Parameters
616-
----------
617-
axis : int, optional
618-
For compatibility with NumPy. Only 0 or None are allowed.
619-
skipna : bool, default True
620-
Exclude NA/null values when showing the result.
621-
*args, **kwargs
622-
Additional arguments and keywords for compatibility with NumPy.
623-
624-
Returns
625-
-------
626-
scalar
627-
Maximum value.
628-
629-
See Also
630-
--------
631-
Index.min : Return the minimum value in an Index.
632-
Series.max : Return the maximum value in a Series.
633-
DataFrame.max : Return the maximum values in a DataFrame.
634-
635-
Examples
636-
--------
637-
>>> idx = pd.Index([3, 2, 1])
638-
>>> idx.max()
639-
3
640-
641-
>>> idx = pd.Index(['c', 'b', 'a'])
642-
>>> idx.max()
643-
'c'
644-
645-
For a MultiIndex, the maximum is determined lexicographically.
646-
647-
>>> idx = pd.MultiIndex.from_product([('a', 'b'), (2, 1)])
648-
>>> idx.max()
649-
('b', 2)
650-
"""
651-
nv.validate_minmax_axis(axis)
652-
nv.validate_max(args, kwargs)
653-
return nanops.nanmax(self._values, skipna=skipna)
654-
655611
@doc(op="max", oppose="min", value="largest")
656612
def argmax(
657613
self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs
@@ -722,50 +678,6 @@ def argmax(
722678
delegate, skipna=skipna
723679
)
724680

725-
def min(self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs):
726-
"""
727-
Return the minimum value of the Index.
728-
729-
Parameters
730-
----------
731-
axis : {None}
732-
Dummy argument for consistency with Series.
733-
skipna : bool, default True
734-
Exclude NA/null values when showing the result.
735-
*args, **kwargs
736-
Additional arguments and keywords for compatibility with NumPy.
737-
738-
Returns
739-
-------
740-
scalar
741-
Minimum value.
742-
743-
See Also
744-
--------
745-
Index.max : Return the maximum value of the object.
746-
Series.min : Return the minimum value in a Series.
747-
DataFrame.min : Return the minimum values in a DataFrame.
748-
749-
Examples
750-
--------
751-
>>> idx = pd.Index([3, 2, 1])
752-
>>> idx.min()
753-
1
754-
755-
>>> idx = pd.Index(['c', 'b', 'a'])
756-
>>> idx.min()
757-
'a'
758-
759-
For a MultiIndex, the minimum is determined lexicographically.
760-
761-
>>> idx = pd.MultiIndex.from_product([('a', 'b'), (2, 1)])
762-
>>> idx.min()
763-
('a', 1)
764-
"""
765-
nv.validate_minmax_axis(axis)
766-
nv.validate_min(args, kwargs)
767-
return nanops.nanmin(self._values, skipna=skipna)
768-
769681
@doc(argmax, op="min", oppose="max", value="smallest")
770682
def argmin(
771683
self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs
@@ -859,30 +771,6 @@ def hasnans(self) -> bool:
859771
# has no attribute "any"
860772
return bool(isna(self).any()) # type: ignore[union-attr]
861773

862-
def isna(self) -> npt.NDArray[np.bool_]:
863-
return isna(self._values)
864-
865-
def _reduce(
866-
self,
867-
op,
868-
name: str,
869-
*,
870-
axis: Axis = 0,
871-
skipna: bool = True,
872-
numeric_only=None,
873-
filter_type=None,
874-
**kwds,
875-
):
876-
"""
877-
Perform the reduction type operation if we can.
878-
"""
879-
func = getattr(self, name, None)
880-
if func is None:
881-
raise TypeError(
882-
f"{type(self).__name__} cannot perform the operation {name}"
883-
)
884-
return func(skipna=skipna, **kwds)
885-
886774
@final
887775
def _map_values(self, mapper, na_action=None, convert: bool = True):
888776
"""

pandas/core/indexes/base.py

+82-4
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135

136136
from pandas.core import (
137137
arraylike,
138+
nanops,
138139
ops,
139140
)
140141
from pandas.core.accessor import CachedAccessor
@@ -6984,8 +6985,46 @@ def argmax(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
69846985
return -1
69856986
return super().argmax(skipna=skipna)
69866987

6987-
@doc(IndexOpsMixin.min)
69886988
def min(self, axis=None, skipna: bool = True, *args, **kwargs):
6989+
"""
6990+
Return the minimum value of the Index.
6991+
6992+
Parameters
6993+
----------
6994+
axis : {None}
6995+
Dummy argument for consistency with Series.
6996+
skipna : bool, default True
6997+
Exclude NA/null values when showing the result.
6998+
*args, **kwargs
6999+
Additional arguments and keywords for compatibility with NumPy.
7000+
7001+
Returns
7002+
-------
7003+
scalar
7004+
Minimum value.
7005+
7006+
See Also
7007+
--------
7008+
Index.max : Return the maximum value of the object.
7009+
Series.min : Return the minimum value in a Series.
7010+
DataFrame.min : Return the minimum values in a DataFrame.
7011+
7012+
Examples
7013+
--------
7014+
>>> idx = pd.Index([3, 2, 1])
7015+
>>> idx.min()
7016+
1
7017+
7018+
>>> idx = pd.Index(['c', 'b', 'a'])
7019+
>>> idx.min()
7020+
'a'
7021+
7022+
For a MultiIndex, the minimum is determined lexicographically.
7023+
7024+
>>> idx = pd.MultiIndex.from_product([('a', 'b'), (2, 1)])
7025+
>>> idx.min()
7026+
('a', 1)
7027+
"""
69897028
nv.validate_min(args, kwargs)
69907029
nv.validate_minmax_axis(axis)
69917030

@@ -7007,10 +7046,49 @@ def min(self, axis=None, skipna: bool = True, *args, **kwargs):
70077046
if not self._is_multi and not isinstance(self._values, np.ndarray):
70087047
return self._values._reduce(name="min", skipna=skipna)
70097048

7010-
return super().min(skipna=skipna)
7049+
return nanops.nanmin(self._values, skipna=skipna)
70117050

7012-
@doc(IndexOpsMixin.max)
70137051
def max(self, axis=None, skipna: bool = True, *args, **kwargs):
7052+
"""
7053+
Return the maximum value of the Index.
7054+
7055+
Parameters
7056+
----------
7057+
axis : int, optional
7058+
For compatibility with NumPy. Only 0 or None are allowed.
7059+
skipna : bool, default True
7060+
Exclude NA/null values when showing the result.
7061+
*args, **kwargs
7062+
Additional arguments and keywords for compatibility with NumPy.
7063+
7064+
Returns
7065+
-------
7066+
scalar
7067+
Maximum value.
7068+
7069+
See Also
7070+
--------
7071+
Index.min : Return the minimum value in an Index.
7072+
Series.max : Return the maximum value in a Series.
7073+
DataFrame.max : Return the maximum values in a DataFrame.
7074+
7075+
Examples
7076+
--------
7077+
>>> idx = pd.Index([3, 2, 1])
7078+
>>> idx.max()
7079+
3
7080+
7081+
>>> idx = pd.Index(['c', 'b', 'a'])
7082+
>>> idx.max()
7083+
'c'
7084+
7085+
For a MultiIndex, the maximum is determined lexicographically.
7086+
7087+
>>> idx = pd.MultiIndex.from_product([('a', 'b'), (2, 1)])
7088+
>>> idx.max()
7089+
('b', 2)
7090+
"""
7091+
70147092
nv.validate_max(args, kwargs)
70157093
nv.validate_minmax_axis(axis)
70167094

@@ -7032,7 +7110,7 @@ def max(self, axis=None, skipna: bool = True, *args, **kwargs):
70327110
if not self._is_multi and not isinstance(self._values, np.ndarray):
70337111
return self._values._reduce(name="max", skipna=skipna)
70347112

7035-
return super().max(skipna=skipna)
7113+
return nanops.nanmax(self._values, skipna=skipna)
70367114

70377115
# --------------------------------------------------------------------
70387116

pandas/core/series.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,10 @@ def wrapper(self):
233233
# Series class
234234

235235

236-
# error: Definition of "max" in base class "IndexOpsMixin" is incompatible with
237-
# definition in base class "NDFrame"
238-
# error: Definition of "min" in base class "IndexOpsMixin" is incompatible with
236+
# error: Cannot override final attribute "ndim" (previously declared in base
237+
# class "NDFrame")
238+
# error: Cannot override final attribute "size" (previously declared in base
239+
# class "NDFrame")
239240
# definition in base class "NDFrame"
240241
class Series(base.IndexOpsMixin, NDFrame): # type: ignore[misc]
241242
"""
@@ -5238,10 +5239,8 @@ def _convert_dtypes(
52385239
return result
52395240

52405241
# error: Cannot determine type of 'isna'
5241-
# error: Return type "Series" of "isna" incompatible with return type "ndarray
5242-
# [Any, dtype[bool_]]" in supertype "IndexOpsMixin"
52435242
@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
5244-
def isna(self) -> Series: # type: ignore[override]
5243+
def isna(self) -> Series:
52455244
return NDFrame.isna(self)
52465245

52475246
# error: Cannot determine type of 'isna'
@@ -5879,8 +5878,7 @@ def all(
58795878
)
58805879

58815880
@doc(make_doc("min", ndim=1))
5882-
# error: Signature of "min" incompatible with supertype "IndexOpsMixin"
5883-
def min( # type: ignore[override]
5881+
def min(
58845882
self,
58855883
axis: Axis | None = 0,
58865884
skipna: bool = True,
@@ -5890,8 +5888,7 @@ def min( # type: ignore[override]
58905888
return NDFrame.min(self, axis, skipna, numeric_only, **kwargs)
58915889

58925890
@doc(make_doc("max", ndim=1))
5893-
# error: Signature of "max" incompatible with supertype "IndexOpsMixin"
5894-
def max( # type: ignore[override]
5891+
def max(
58955892
self,
58965893
axis: Axis | None = 0,
58975894
skipna: bool = True,

0 commit comments

Comments
 (0)