Skip to content

Make Index.all compatible with numpy.all #40180

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 8 commits into from
Mar 5, 2021
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ Reshaping
- Bug in :meth:`DataFrame.append` returning incorrect dtypes with combinations of ``ExtensionDtype`` dtypes (:issue:`39454`)
- Bug in :meth:`DataFrame.append` returning incorrect dtypes with combinations of ``datetime64`` and ``timedelta64`` dtypes (:issue:`39574`)
- Bug in :meth:`DataFrame.pivot_table` returning a ``MultiIndex`` for a single value when operating on and empty ``DataFrame`` (:issue:`13483`)
- Allow :class:`Index` to be passed to the :func:`numpy.all` function (:issue:`40180`)
-

Sparse
^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pandas/compat/numpy/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def validate_cum_func_with_skipna(skipna, args, kwargs, name):
ALLANY_DEFAULTS["dtype"] = None
ALLANY_DEFAULTS["out"] = None
ALLANY_DEFAULTS["keepdims"] = False
ALLANY_DEFAULTS["axis"] = None
validate_all = CompatValidator(
ALLANY_DEFAULTS, fname="all", method="both", max_fname_arg_count=1
)
Expand Down
15 changes: 7 additions & 8 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5964,9 +5964,9 @@ def any(self, *args, **kwargs):
Parameters
----------
*args
These parameters will be passed to numpy.any.
Required for compatibility with numpy.
**kwargs
These parameters will be passed to numpy.any.
Required for compatibility with numpy.

Returns
-------
Expand All @@ -5993,20 +5993,20 @@ def any(self, *args, **kwargs):
>>> index.any()
False
"""
# FIXME: docstr inaccurate, args/kwargs not passed
nv.validate_any(args, kwargs)
self._maybe_disable_logical_methods("any")
return np.any(self.values)

def all(self):
def all(self, *args, **kwargs):
"""
Return whether all elements are Truthy.

Parameters
----------
*args
These parameters will be passed to numpy.all.
Required for compatibility with numpy.
**kwargs
These parameters will be passed to numpy.all.
Required for compatibility with numpy.

Returns
-------
Expand Down Expand Up @@ -6050,8 +6050,7 @@ def all(self):
>>> pd.Index([0, 0, 0]).any()
False
"""
# FIXME: docstr inaccurate, args/kwargs not passed

nv.validate_all(args, kwargs)
self._maybe_disable_logical_methods("all")
return np.all(self.values)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,15 @@ def test_all_any(self):
s = Series(["abc", True])
assert "abc" == s.any() # 'abc' || True => 'abc'

@pytest.mark.parametrize("klass", [Index, Series])
def test_numpy_all_any(self, klass):
# GH#40180
idx = klass([0, 1, 2])
assert not np.all(idx)
assert np.any(idx)
idx = Index([1, 2, 3])
assert np.all(idx)

def test_all_any_params(self):
# Check skipna, with implicit 'object' dtype.
s1 = Series([np.nan, True])
Expand Down