Skip to content

BUG: Ensure consistent kwarg only restriction on df.any and df.all #57396

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

Closed
wants to merge 4 commits into from
Closed
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: 1 addition & 1 deletion doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/methods/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1506,6 +1506,27 @@ def test_any_all_object(self):
result = np.any(DataFrame(columns=["a", "b"])).item()
assert result is False

@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):
getattr(df, method)(0)

with pytest.raises(TypeError, match=msg):
getattr(df, method)(0, bool_only=True, skipna=False)

with pytest.raises(TypeError, match=msg):
getattr(df, method)(0, True, False)

with pytest.raises(TypeError, match=msg):
getattr(df, method)(0, bool_only=True)

# Ensure that it works with only keyword arguments
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)
df._consolidate_inplace()
Expand Down