From aa3cfec6df3377d7e0456c906c23f8ed3db18b16 Mon Sep 17 00:00:00 2001 From: Paul McCarthy Date: Tue, 2 Mar 2021 20:11:52 +0000 Subject: [PATCH 1/8] REF: numpy.any and all accept an "axis" argument --- pandas/compat/numpy/function.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/compat/numpy/function.py b/pandas/compat/numpy/function.py index 8934a02a8f5bc..3f56ecd640774 100644 --- a/pandas/compat/numpy/function.py +++ b/pandas/compat/numpy/function.py @@ -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 ) From ea576e5946b70427b0ea64e477abcb41f6d438e8 Mon Sep 17 00:00:00 2001 From: Paul McCarthy Date: Tue, 2 Mar 2021 20:13:16 +0000 Subject: [PATCH 2/8] BUG: Make Index.all accept args/kwargs, for compatibility with numpy.all --- pandas/core/indexes/base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 44c9b33ae51c7..6feafd1328eae 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5994,10 +5994,11 @@ def any(self, *args, **kwargs): 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. @@ -6052,6 +6053,7 @@ def all(self): """ # FIXME: docstr inaccurate, args/kwargs not passed + nv.validate_all(args, kwargs) self._maybe_disable_logical_methods("all") return np.all(self.values) From cf4c4159d7bfb4ce919cf2e724f50a3eb8f10c00 Mon Sep 17 00:00:00 2001 From: Paul McCarthy Date: Tue, 2 Mar 2021 20:13:55 +0000 Subject: [PATCH 3/8] DOC: update docstrings for Index.any/all --- pandas/core/indexes/base.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 6feafd1328eae..7f5e7e3a32f14 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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 ------- @@ -5993,7 +5993,6 @@ 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) @@ -6005,9 +6004,9 @@ def all(self, *args, **kwargs): 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 ------- @@ -6051,8 +6050,6 @@ def all(self, *args, **kwargs): >>> 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) From b0c1e409dbd159842d22c0bc400aeee1d2a78ed1 Mon Sep 17 00:00:00 2001 From: Paul McCarthy Date: Tue, 2 Mar 2021 20:14:08 +0000 Subject: [PATCH 4/8] TEST: Test Index.any/all compat with numpy --- pandas/tests/reductions/test_reductions.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index e3145e0cc5c9f..74e566262023f 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -550,6 +550,14 @@ def test_min_max_categorical(self): assert ci.min() == "c" assert ci.max() == "b" + @pytest.mark.parametrize("op", ["any", "all"]) + def test_numpy_any_all(self, op): + idx = Index([0, 1, 2]) + assert not np.all(idx) + assert np.any(idx) + idx = Index([1, 2, 3]) + assert np.all(idx) + class TestSeriesReductions: # Note: the name TestSeriesReductions indicates these tests From 7a3b452a6fa21a54a81724a22135b2a490c67a6f Mon Sep 17 00:00:00 2001 From: Paul McCarthy Date: Tue, 2 Mar 2021 20:14:21 +0000 Subject: [PATCH 5/8] DOC: changelog --- doc/source/whatsnew/v1.2.4.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.4.rst b/doc/source/whatsnew/v1.2.4.rst index 790ff4c78cad6..0fd48ba21e54e 100644 --- a/doc/source/whatsnew/v1.2.4.rst +++ b/doc/source/whatsnew/v1.2.4.rst @@ -35,7 +35,7 @@ Bug fixes Other ~~~~~ -- +- Allow :class:`Index` to be passed to the numpy ``all`` function. - .. --------------------------------------------------------------------------- From 8348f8d379a9442b82244afd75bab61e1499b9eb Mon Sep 17 00:00:00 2001 From: Paul McCarthy Date: Tue, 2 Mar 2021 20:16:02 +0000 Subject: [PATCH 6/8] TEST: Unnecessary paramtrize --- pandas/tests/reductions/test_reductions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 74e566262023f..20a0043b1848c 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -550,8 +550,7 @@ def test_min_max_categorical(self): assert ci.min() == "c" assert ci.max() == "b" - @pytest.mark.parametrize("op", ["any", "all"]) - def test_numpy_any_all(self, op): + def test_numpy_any_all(self): idx = Index([0, 1, 2]) assert not np.all(idx) assert np.any(idx) From 1cd46e943a6f16f706ee72be7d1e362663aaef7f Mon Sep 17 00:00:00 2001 From: Paul McCarthy Date: Thu, 4 Mar 2021 15:38:37 +0000 Subject: [PATCH 7/8] DOC: Move to v1.3 changelog --- doc/source/whatsnew/v1.2.4.rst | 2 +- doc/source/whatsnew/v1.3.0.rst | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.4.rst b/doc/source/whatsnew/v1.2.4.rst index 0fd48ba21e54e..790ff4c78cad6 100644 --- a/doc/source/whatsnew/v1.2.4.rst +++ b/doc/source/whatsnew/v1.2.4.rst @@ -35,7 +35,7 @@ Bug fixes Other ~~~~~ -- Allow :class:`Index` to be passed to the numpy ``all`` function. +- - .. --------------------------------------------------------------------------- diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index f7204ceb9d412..7a23fb801a29a 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -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 ^^^^^^ From c102edc84297d7f3888cd44308eac250054ecd45 Mon Sep 17 00:00:00 2001 From: Paul McCarthy Date: Thu, 4 Mar 2021 15:41:33 +0000 Subject: [PATCH 8/8] TEST: Relocate unit test to a more sensible location, test both Index and Series compat --- pandas/tests/reductions/test_reductions.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 20a0043b1848c..77b549e675a8d 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -550,13 +550,6 @@ def test_min_max_categorical(self): assert ci.min() == "c" assert ci.max() == "b" - def test_numpy_any_all(self): - idx = Index([0, 1, 2]) - assert not np.all(idx) - assert np.any(idx) - idx = Index([1, 2, 3]) - assert np.all(idx) - class TestSeriesReductions: # Note: the name TestSeriesReductions indicates these tests @@ -905,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])