Skip to content

Commit 539b813

Browse files
Make Index.all compatible with numpy.all (#40180)
1 parent 782dc18 commit 539b813

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ Reshaping
580580
- Bug in :meth:`DataFrame.append` returning incorrect dtypes with combinations of ``ExtensionDtype`` dtypes (:issue:`39454`)
581581
- Bug in :meth:`DataFrame.append` returning incorrect dtypes with combinations of ``datetime64`` and ``timedelta64`` dtypes (:issue:`39574`)
582582
- Bug in :meth:`DataFrame.pivot_table` returning a ``MultiIndex`` for a single value when operating on and empty ``DataFrame`` (:issue:`13483`)
583+
- Allow :class:`Index` to be passed to the :func:`numpy.all` function (:issue:`40180`)
584+
-
583585

584586
Sparse
585587
^^^^^^

pandas/compat/numpy/function.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def validate_cum_func_with_skipna(skipna, args, kwargs, name):
212212
ALLANY_DEFAULTS["dtype"] = None
213213
ALLANY_DEFAULTS["out"] = None
214214
ALLANY_DEFAULTS["keepdims"] = False
215+
ALLANY_DEFAULTS["axis"] = None
215216
validate_all = CompatValidator(
216217
ALLANY_DEFAULTS, fname="all", method="both", max_fname_arg_count=1
217218
)

pandas/core/indexes/base.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5964,9 +5964,9 @@ def any(self, *args, **kwargs):
59645964
Parameters
59655965
----------
59665966
*args
5967-
These parameters will be passed to numpy.any.
5967+
Required for compatibility with numpy.
59685968
**kwargs
5969-
These parameters will be passed to numpy.any.
5969+
Required for compatibility with numpy.
59705970
59715971
Returns
59725972
-------
@@ -5993,20 +5993,20 @@ def any(self, *args, **kwargs):
59935993
>>> index.any()
59945994
False
59955995
"""
5996-
# FIXME: docstr inaccurate, args/kwargs not passed
5996+
nv.validate_any(args, kwargs)
59975997
self._maybe_disable_logical_methods("any")
59985998
return np.any(self.values)
59995999

6000-
def all(self):
6000+
def all(self, *args, **kwargs):
60016001
"""
60026002
Return whether all elements are Truthy.
60036003
60046004
Parameters
60056005
----------
60066006
*args
6007-
These parameters will be passed to numpy.all.
6007+
Required for compatibility with numpy.
60086008
**kwargs
6009-
These parameters will be passed to numpy.all.
6009+
Required for compatibility with numpy.
60106010
60116011
Returns
60126012
-------
@@ -6050,8 +6050,7 @@ def all(self):
60506050
>>> pd.Index([0, 0, 0]).any()
60516051
False
60526052
"""
6053-
# FIXME: docstr inaccurate, args/kwargs not passed
6054-
6053+
nv.validate_all(args, kwargs)
60556054
self._maybe_disable_logical_methods("all")
60566055
return np.all(self.values)
60576056

pandas/tests/reductions/test_reductions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,15 @@ def test_all_any(self):
898898
s = Series(["abc", True])
899899
assert "abc" == s.any() # 'abc' || True => 'abc'
900900

901+
@pytest.mark.parametrize("klass", [Index, Series])
902+
def test_numpy_all_any(self, klass):
903+
# GH#40180
904+
idx = klass([0, 1, 2])
905+
assert not np.all(idx)
906+
assert np.any(idx)
907+
idx = Index([1, 2, 3])
908+
assert np.all(idx)
909+
901910
def test_all_any_params(self):
902911
# Check skipna, with implicit 'object' dtype.
903912
s1 = Series([np.nan, True])

0 commit comments

Comments
 (0)