From 4f9e2075b63a9577c19b0976ed2edea39962b007 Mon Sep 17 00:00:00 2001 From: aidoskanapyanov Date: Tue, 13 Feb 2024 10:51:08 +0600 Subject: [PATCH 1/4] BUG: Ensure consistent kwarg only restriction on df.any and df.all --- pandas/core/frame.py | 1 + pandas/tests/frame/test_reductions.py | 33 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3a207a7e57fe8..170a21d28fc8a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11331,6 +11331,7 @@ def all( @doc(make_doc("all", ndim=2)) def all( self, + *, axis: Axis | None = 0, bool_only: bool = False, skipna: bool = True, diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 63c15fab76562..6e91f664383c8 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -1506,6 +1506,39 @@ def test_any_all_object(self): result = np.any(DataFrame(columns=["a", "b"])).item() assert result is False + def test_any_all_take_kwargs_only(self): + # GH 57087 + df = DataFrame({"x": [1, 2, 3], "y": [1, 2, 3]}) + msg = "takes 1 positional argument but" + + with pytest.raises(TypeError, match=msg): + df.all(0) + + with pytest.raises(TypeError, match=msg): + df.all(0, bool_only=True, skipna=False) + + with pytest.raises(TypeError, match=msg): + df.all(0, True, False) + + with pytest.raises(TypeError, match=msg): + df.all(0, bool_only=True) + + with pytest.raises(TypeError, match=msg): + df.any(0) + + with pytest.raises(TypeError, match=msg): + df.any(0, bool_only=True, skipna=False) + + with pytest.raises(TypeError, match=msg): + df.any(0, True, False) + + with pytest.raises(TypeError, match=msg): + df.any(0, bool_only=True) + + # Ensure that it works with only keyword arguments + df.all(axis=0, bool_only=True, skipna=False) + df.any(axis=0, bool_only=True, skipna=False) + def test_any_all_object_bool_only(self): df = DataFrame({"A": ["foo", 2], "B": [True, False]}).astype(object) df._consolidate_inplace() From 111dcc9e9d9fce7c70e786ee38264f6911778af7 Mon Sep 17 00:00:00 2001 From: aidoskanapyanov Date: Tue, 13 Feb 2024 11:06:55 +0600 Subject: [PATCH 2/4] Parameterize test --- pandas/tests/frame/test_reductions.py | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 6e91f664383c8..7da99a6e80179 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -1506,38 +1506,26 @@ def test_any_all_object(self): result = np.any(DataFrame(columns=["a", "b"])).item() assert result is False - def test_any_all_take_kwargs_only(self): + @pytest.mark.parametrize("method", ["all", "any"]) + def test_any_all_take_kwargs_only(self, method): # GH 57087 df = DataFrame({"x": [1, 2, 3], "y": [1, 2, 3]}) msg = "takes 1 positional argument but" with pytest.raises(TypeError, match=msg): - df.all(0) + getattr(df, method)(0) with pytest.raises(TypeError, match=msg): - df.all(0, bool_only=True, skipna=False) + getattr(df, method)(0, bool_only=True, skipna=False) with pytest.raises(TypeError, match=msg): - df.all(0, True, False) + getattr(df, method)(0, True, False) with pytest.raises(TypeError, match=msg): - df.all(0, bool_only=True) - - with pytest.raises(TypeError, match=msg): - df.any(0) - - with pytest.raises(TypeError, match=msg): - df.any(0, bool_only=True, skipna=False) - - with pytest.raises(TypeError, match=msg): - df.any(0, True, False) - - with pytest.raises(TypeError, match=msg): - df.any(0, bool_only=True) + getattr(df, method)(0, bool_only=True) # Ensure that it works with only keyword arguments - df.all(axis=0, bool_only=True, skipna=False) - df.any(axis=0, bool_only=True, skipna=False) + getattr(df, method)(axis=0, bool_only=True, skipna=False) def test_any_all_object_bool_only(self): df = DataFrame({"A": ["foo", 2], "B": [True, False]}).astype(object) From 2db18772a20e51c0558d932b49b3ee5297004cad Mon Sep 17 00:00:00 2001 From: aidoskanapyanov Date: Tue, 13 Feb 2024 11:30:26 +0600 Subject: [PATCH 3/4] Update unit tests w/ positional df.all --- pandas/tests/frame/methods/test_asof.py | 6 +++--- pandas/tests/frame/test_reductions.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/frame/methods/test_asof.py b/pandas/tests/frame/methods/test_asof.py index 029aa3a5b8f05..c510ef78d03aa 100644 --- a/pandas/tests/frame/methods/test_asof.py +++ b/pandas/tests/frame/methods/test_asof.py @@ -36,18 +36,18 @@ def test_basic(self, date_range_frame): dates = date_range("1/1/1990", periods=N * 3, freq="25s") result = df.asof(dates) - assert result.notna().all(1).all() + assert result.notna().all(axis=1).all() lb = df.index[14] ub = df.index[30] dates = list(dates) result = df.asof(dates) - assert result.notna().all(1).all() + assert result.notna().all(axis=1).all() mask = (result.index >= lb) & (result.index < ub) rs = result[mask] - assert (rs == 14).all(1).all() + assert (rs == 14).all(axis=1).all() def test_subset(self, date_range_frame): N = 10 diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 7da99a6e80179..394e7e90ea1e5 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -1329,11 +1329,11 @@ def test_any_all_extra(self): result = df[["A", "B"]].any(axis=1, bool_only=True) tm.assert_series_equal(result, expected) - result = df.all(1) + result = df.all(axis=1) expected = Series([True, False, False], index=["a", "b", "c"]) tm.assert_series_equal(result, expected) - result = df.all(1, bool_only=True) + result = df.all(axis=1, bool_only=True) tm.assert_series_equal(result, expected) # Axis is None From 79ccc7fb6d324ea14f289a32eff033097cd64fc1 Mon Sep 17 00:00:00 2001 From: aidoskanapyanov Date: Tue, 13 Feb 2024 11:50:27 +0600 Subject: [PATCH 4/4] Update docs where df.all is called without kwargs --- doc/source/user_guide/indexing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst index 24cdbad41fe60..aaffdfbdd6d43 100644 --- a/doc/source/user_guide/indexing.rst +++ b/doc/source/user_guide/indexing.rst @@ -948,7 +948,7 @@ To select a row where each column meets its own criterion: values = {'ids': ['a', 'b'], 'ids2': ['a', 'c'], 'vals': [1, 3]} - row_mask = df.isin(values).all(1) + row_mask = df.isin(values).all(axis=1) df[row_mask]