Skip to content

Commit c04e231

Browse files
authored
TYP: define Index.any, Index.all non-dynamically (#36929)
1 parent 60a7d2a commit c04e231

File tree

9 files changed

+63
-91
lines changed

9 files changed

+63
-91
lines changed

pandas/core/indexes/base.py

+61-70
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from datetime import datetime
33
from itertools import zip_longest
44
import operator
5-
from textwrap import dedent
65
from typing import (
76
TYPE_CHECKING,
87
Any,
@@ -30,7 +29,7 @@
3029
from pandas.compat import set_function_name
3130
from pandas.compat.numpy import function as nv
3231
from pandas.errors import DuplicateLabelError, InvalidIndexError
33-
from pandas.util._decorators import Appender, Substitution, cache_readonly, doc
32+
from pandas.util._decorators import Appender, cache_readonly, doc
3433

3534
from pandas.core.dtypes import concat as _concat
3635
from pandas.core.dtypes.cast import (
@@ -61,6 +60,7 @@
6160
is_signed_integer_dtype,
6261
is_timedelta64_dtype,
6362
is_unsigned_integer_dtype,
63+
needs_i8_conversion,
6464
pandas_dtype,
6565
validate_all_hashable,
6666
)
@@ -5447,28 +5447,61 @@ def _add_numeric_methods(cls):
54475447
cls._add_numeric_methods_unary()
54485448
cls._add_numeric_methods_binary()
54495449

5450-
@classmethod
5451-
def _add_logical_methods(cls):
5452-
"""
5453-
Add in logical methods.
5450+
def any(self, *args, **kwargs):
54545451
"""
5455-
_doc = """
5456-
%(desc)s
5452+
Return whether any element is Truthy.
54575453
54585454
Parameters
54595455
----------
54605456
*args
5461-
These parameters will be passed to numpy.%(outname)s.
5457+
These parameters will be passed to numpy.any.
54625458
**kwargs
5463-
These parameters will be passed to numpy.%(outname)s.
5459+
These parameters will be passed to numpy.any.
54645460
54655461
Returns
54665462
-------
5467-
%(outname)s : bool or array_like (if axis is specified)
5468-
A single element array_like may be converted to bool."""
5463+
any : bool or array_like (if axis is specified)
5464+
A single element array_like may be converted to bool.
54695465
5470-
_index_shared_docs["index_all"] = dedent(
5471-
"""
5466+
See Also
5467+
--------
5468+
Index.all : Return whether all elements are True.
5469+
Series.all : Return whether all elements are True.
5470+
5471+
Notes
5472+
-----
5473+
Not a Number (NaN), positive infinity and negative infinity
5474+
evaluate to True because these are not equal to zero.
5475+
5476+
Examples
5477+
--------
5478+
>>> index = pd.Index([0, 1, 2])
5479+
>>> index.any()
5480+
True
5481+
5482+
>>> index = pd.Index([0, 0, 0])
5483+
>>> index.any()
5484+
False
5485+
"""
5486+
# FIXME: docstr inaccurate, args/kwargs not passed
5487+
self._maybe_disable_logical_methods("any")
5488+
return np.any(self.values)
5489+
5490+
def all(self):
5491+
"""
5492+
Return whether all elements are Truthy.
5493+
5494+
Parameters
5495+
----------
5496+
*args
5497+
These parameters will be passed to numpy.all.
5498+
**kwargs
5499+
These parameters will be passed to numpy.all.
5500+
5501+
Returns
5502+
-------
5503+
all : bool or array_like (if axis is specified)
5504+
A single element array_like may be converted to bool.
54725505
54735506
See Also
54745507
--------
@@ -5507,65 +5540,24 @@ def _add_logical_methods(cls):
55075540
>>> pd.Index([0, 0, 0]).any()
55085541
False
55095542
"""
5510-
)
5511-
5512-
_index_shared_docs["index_any"] = dedent(
5513-
"""
5514-
5515-
See Also
5516-
--------
5517-
Index.all : Return whether all elements are True.
5518-
Series.all : Return whether all elements are True.
5543+
# FIXME: docstr inaccurate, args/kwargs not passed
55195544

5520-
Notes
5521-
-----
5522-
Not a Number (NaN), positive infinity and negative infinity
5523-
evaluate to True because these are not equal to zero.
5545+
self._maybe_disable_logical_methods("all")
5546+
return np.all(self.values)
55245547

5525-
Examples
5526-
--------
5527-
>>> index = pd.Index([0, 1, 2])
5528-
>>> index.any()
5529-
True
5530-
5531-
>>> index = pd.Index([0, 0, 0])
5532-
>>> index.any()
5533-
False
5548+
def _maybe_disable_logical_methods(self, opname: str_t):
55345549
"""
5535-
)
5536-
5537-
def _make_logical_function(name: str_t, desc: str_t, f):
5538-
@Substitution(outname=name, desc=desc)
5539-
@Appender(_index_shared_docs["index_" + name])
5540-
@Appender(_doc)
5541-
def logical_func(self, *args, **kwargs):
5542-
result = f(self.values)
5543-
if (
5544-
isinstance(result, (np.ndarray, ABCSeries, Index))
5545-
and result.ndim == 0
5546-
):
5547-
# return NumPy type
5548-
return result.dtype.type(result.item())
5549-
else: # pragma: no cover
5550-
return result
5551-
5552-
logical_func.__name__ = name
5553-
return logical_func
5554-
5555-
cls.all = _make_logical_function(
5556-
"all", "Return whether all elements are True.", np.all
5557-
)
5558-
cls.any = _make_logical_function(
5559-
"any", "Return whether any element is True.", np.any
5560-
)
5561-
5562-
@classmethod
5563-
def _add_logical_methods_disabled(cls):
5550+
raise if this Index subclass does not support any or all.
55645551
"""
5565-
Add in logical methods to disable.
5566-
"""
5567-
cls.all = make_invalid_op("all")
5568-
cls.any = make_invalid_op("any")
5552+
if (
5553+
isinstance(self, ABCMultiIndex)
5554+
or needs_i8_conversion(self.dtype)
5555+
or is_interval_dtype(self.dtype)
5556+
or is_categorical_dtype(self.dtype)
5557+
or is_float_dtype(self.dtype)
5558+
):
5559+
# This call will raise
5560+
make_invalid_op(opname)(self)
55695561

55705562
@property
55715563
def shape(self):
@@ -5579,7 +5571,6 @@ def shape(self):
55795571

55805572

55815573
Index._add_numeric_methods()
5582-
Index._add_logical_methods()
55835574

55845575

55855576
def ensure_index_from_sequences(sequences, names=None):

pandas/core/indexes/category.py

-3
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,3 @@ def _wrap_joined_index(
721721
name = get_op_result_name(self, other)
722722
cat = self._data._from_backing_data(joined)
723723
return type(self)._simple_new(cat, name=name)
724-
725-
726-
CategoricalIndex._add_logical_methods_disabled()

pandas/core/indexes/datetimes.py

-3
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,6 @@ def indexer_between_time(
896896
return mask.nonzero()[0]
897897

898898

899-
DatetimeIndex._add_logical_methods_disabled()
900-
901-
902899
def date_range(
903900
start=None,
904901
end=None,

pandas/core/indexes/interval.py

-3
Original file line numberDiff line numberDiff line change
@@ -1120,9 +1120,6 @@ def __ge__(self, other):
11201120
return Index.__ge__(self, other)
11211121

11221122

1123-
IntervalIndex._add_logical_methods_disabled()
1124-
1125-
11261123
def _is_valid_endpoint(endpoint) -> bool:
11271124
"""
11281125
Helper for interval_range to check if start/end are valid types.

pandas/core/indexes/multi.py

-1
Original file line numberDiff line numberDiff line change
@@ -3723,7 +3723,6 @@ def _add_numeric_methods_disabled(cls):
37233723

37243724
MultiIndex._add_numeric_methods_disabled()
37253725
MultiIndex._add_numeric_methods_add_sub_disabled()
3726-
MultiIndex._add_logical_methods_disabled()
37273726

37283727

37293728
def sparsify_labels(label_list, start: int = 0, sentinel=""):

pandas/core/indexes/numeric.py

-3
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ def _can_union_without_object_cast(self, other) -> bool:
276276

277277

278278
Int64Index._add_numeric_methods()
279-
Int64Index._add_logical_methods()
280279

281280
_uint64_descr_args = dict(
282281
klass="UInt64Index", ltype="unsigned integer", dtype="uint64", extra=""
@@ -323,7 +322,6 @@ def _can_union_without_object_cast(self, other) -> bool:
323322

324323

325324
UInt64Index._add_numeric_methods()
326-
UInt64Index._add_logical_methods()
327325

328326
_float64_descr_args = dict(
329327
klass="Float64Index", dtype="float64", ltype="float", extra=""
@@ -430,4 +428,3 @@ def _can_union_without_object_cast(self, other) -> bool:
430428

431429

432430
Float64Index._add_numeric_methods()
433-
Float64Index._add_logical_methods_disabled()

pandas/core/indexes/period.py

-3
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,6 @@ def memory_usage(self, deep: bool = False) -> int:
719719
return result
720720

721721

722-
PeriodIndex._add_logical_methods_disabled()
723-
724-
725722
def period_range(
726723
start=None, end=None, periods=None, freq=None, name=None
727724
) -> PeriodIndex:

pandas/core/indexes/range.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,10 @@ def __floordiv__(self, other):
804804
# --------------------------------------------------------------------
805805
# Reductions
806806

807-
def all(self) -> bool:
807+
def all(self, *args, **kwargs) -> bool:
808808
return 0 not in self._range
809809

810-
def any(self) -> bool:
810+
def any(self, *args, **kwargs) -> bool:
811811
return any(self._range)
812812

813813
# --------------------------------------------------------------------

pandas/core/indexes/timedeltas.py

-3
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,6 @@ def inferred_type(self) -> str:
261261
return "timedelta64"
262262

263263

264-
TimedeltaIndex._add_logical_methods_disabled()
265-
266-
267264
def timedelta_range(
268265
start=None, end=None, periods=None, freq=None, name=None, closed=None
269266
) -> TimedeltaIndex:

0 commit comments

Comments
 (0)