2
2
from datetime import datetime
3
3
from itertools import zip_longest
4
4
import operator
5
- from textwrap import dedent
6
5
from typing import (
7
6
TYPE_CHECKING ,
8
7
Any ,
30
29
from pandas .compat import set_function_name
31
30
from pandas .compat .numpy import function as nv
32
31
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
34
33
35
34
from pandas .core .dtypes import concat as _concat
36
35
from pandas .core .dtypes .cast import (
61
60
is_signed_integer_dtype ,
62
61
is_timedelta64_dtype ,
63
62
is_unsigned_integer_dtype ,
63
+ needs_i8_conversion ,
64
64
pandas_dtype ,
65
65
validate_all_hashable ,
66
66
)
@@ -5447,28 +5447,61 @@ def _add_numeric_methods(cls):
5447
5447
cls ._add_numeric_methods_unary ()
5448
5448
cls ._add_numeric_methods_binary ()
5449
5449
5450
- @classmethod
5451
- def _add_logical_methods (cls ):
5452
- """
5453
- Add in logical methods.
5450
+ def any (self , * args , ** kwargs ):
5454
5451
"""
5455
- _doc = """
5456
- %(desc)s
5452
+ Return whether any element is Truthy.
5457
5453
5458
5454
Parameters
5459
5455
----------
5460
5456
*args
5461
- These parameters will be passed to numpy.%(outname)s .
5457
+ These parameters will be passed to numpy.any .
5462
5458
**kwargs
5463
- These parameters will be passed to numpy.%(outname)s .
5459
+ These parameters will be passed to numpy.any .
5464
5460
5465
5461
Returns
5466
5462
-------
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.
5469
5465
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.
5472
5505
5473
5506
See Also
5474
5507
--------
@@ -5507,65 +5540,24 @@ def _add_logical_methods(cls):
5507
5540
>>> pd.Index([0, 0, 0]).any()
5508
5541
False
5509
5542
"""
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
5519
5544
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 )
5524
5547
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 ):
5534
5549
"""
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.
5564
5551
"""
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 )
5569
5561
5570
5562
@property
5571
5563
def shape (self ):
@@ -5579,7 +5571,6 @@ def shape(self):
5579
5571
5580
5572
5581
5573
Index ._add_numeric_methods ()
5582
- Index ._add_logical_methods ()
5583
5574
5584
5575
5585
5576
def ensure_index_from_sequences (sequences , names = None ):
0 commit comments