Skip to content

Commit d54bd78

Browse files
authored
DEPR: NDFrame.bool (#51756)
1 parent 7ee7453 commit d54bd78

File tree

9 files changed

+82
-52
lines changed

9 files changed

+82
-52
lines changed

doc/source/user_guide/basics.rst

-10
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,6 @@ You can test if a pandas object is empty, via the :attr:`~DataFrame.empty` prope
329329
df.empty
330330
pd.DataFrame(columns=list("ABC")).empty
331331
332-
To evaluate single-element pandas objects in a boolean context, use the method
333-
:meth:`~DataFrame.bool`:
334-
335-
.. ipython:: python
336-
337-
pd.Series([True]).bool()
338-
pd.Series([False]).bool()
339-
pd.DataFrame([[True]]).bool()
340-
pd.DataFrame([[False]]).bool()
341-
342332
.. warning::
343333

344334
You might be tempted to do the following:

doc/source/user_guide/gotchas.rst

-10
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,6 @@ Below is how to check if any of the values are ``True``:
121121
if pd.Series([False, True, False]).any():
122122
print("I am any")
123123
124-
To evaluate single-element pandas objects in a boolean context, use the method
125-
:meth:`~DataFrame.bool`:
126-
127-
.. ipython:: python
128-
129-
pd.Series([True]).bool()
130-
pd.Series([False]).bool()
131-
pd.DataFrame([[True]]).bool()
132-
pd.DataFrame([[False]]).bool()
133-
134124
Bitwise boolean
135125
~~~~~~~~~~~~~~~
136126

doc/source/whatsnew/v0.13.0.rst

+9-5
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,16 @@ API changes
153153
154154
Added the ``.bool()`` method to ``NDFrame`` objects to facilitate evaluating of single-element boolean Series:
155155

156-
.. ipython:: python
156+
.. code-block:: python
157157
158-
pd.Series([True]).bool()
159-
pd.Series([False]).bool()
160-
pd.DataFrame([[True]]).bool()
161-
pd.DataFrame([[False]]).bool()
158+
>>> pd.Series([True]).bool()
159+
True
160+
>>> pd.Series([False]).bool()
161+
False
162+
>>> pd.DataFrame([[True]]).bool()
163+
True
164+
>>> pd.DataFrame([[False]]).bool()
165+
False
162166
163167
- All non-Index NDFrames (``Series``, ``DataFrame``, ``Panel``, ``Panel4D``,
164168
``SparsePanel``, etc.), now support the entire set of arithmetic operators

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Deprecations
125125
- Deprecated the 'axis' keyword in :meth:`.GroupBy.idxmax`, :meth:`.GroupBy.idxmin`, :meth:`.GroupBy.fillna`, :meth:`.GroupBy.take`, :meth:`.GroupBy.skew`, :meth:`.GroupBy.rank`, :meth:`.GroupBy.cumprod`, :meth:`.GroupBy.cumsum`, :meth:`.GroupBy.cummax`, :meth:`.GroupBy.cummin`, :meth:`.GroupBy.pct_change`, :meth:`GroupBy.diff`, :meth:`.GroupBy.shift`, and :meth:`DataFrameGroupBy.corrwith`; for ``axis=1`` operate on the underlying :class:`DataFrame` instead (:issue:`50405`, :issue:`51046`)
126126
- Deprecated passing a dictionary to :meth:`.SeriesGroupBy.agg`; pass a list of aggregations instead (:issue:`50684`)
127127
- Deprecated logical operations (``|``, ``&``, ``^``) between pandas objects and dtype-less sequences (e.g. ``list``, ``tuple``), wrap a sequence in a :class:`Series` or numpy array before operating instead (:issue:`51521`)
128+
- Deprecated the methods :meth:`Series.bool` and :meth:`DataFrame.bool` (:issue:`51749`)
128129
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
129130
- Deprecated parameter ``convert_type`` in :meth:`Series.apply` (:issue:`52140`)
130131
-

pandas/conftest.py

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ def pytest_collection_modifyitems(items, config) -> None:
137137
ignored_doctest_warnings = [
138138
# Docstring divides by zero to show behavior difference
139139
("missing.mask_zero_div_zero", "divide by zero encountered"),
140+
(
141+
"pandas.core.generic.NDFrame.bool",
142+
"(Series|DataFrame).bool is now deprecated and will be removed "
143+
"in future version of pandas",
144+
),
140145
]
141146

142147
for item in items:

pandas/core/generic.py

+7
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,13 @@ def bool(self) -> bool_t:
15131513
>>> pd.DataFrame({'col': [False]}).bool()
15141514
False
15151515
"""
1516+
1517+
warnings.warn(
1518+
f"{type(self).__name__}.bool is now deprecated and will be removed "
1519+
"in future version of pandas",
1520+
FutureWarning,
1521+
stacklevel=find_stack_level(),
1522+
)
15161523
v = self.squeeze()
15171524
if isinstance(v, (bool, np.bool_)):
15181525
return bool(v)

pandas/tests/generic/test_frame.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,27 @@ def test_set_axis_name_mi(self, func):
4747

4848
def test_nonzero_single_element(self):
4949
# allow single item via bool method
50+
msg_warn = (
51+
"DataFrame.bool is now deprecated and will be removed "
52+
"in future version of pandas"
53+
)
5054
df = DataFrame([[True]])
51-
assert df.bool()
55+
df1 = DataFrame([[False]])
56+
with tm.assert_produces_warning(FutureWarning, match=msg_warn):
57+
assert df.bool()
5258

53-
df = DataFrame([[False]])
54-
assert not df.bool()
59+
with tm.assert_produces_warning(FutureWarning, match=msg_warn):
60+
assert not df1.bool()
5561

5662
df = DataFrame([[False, False]])
57-
msg = "The truth value of a DataFrame is ambiguous"
58-
with pytest.raises(ValueError, match=msg):
59-
df.bool()
60-
with pytest.raises(ValueError, match=msg):
63+
msg_err = "The truth value of a DataFrame is ambiguous"
64+
with pytest.raises(ValueError, match=msg_err):
6165
bool(df)
6266

67+
with tm.assert_produces_warning(FutureWarning, match=msg_warn):
68+
with pytest.raises(ValueError, match=msg_err):
69+
df.bool()
70+
6371
def test_metadata_propagation_indiv_groupby(self):
6472
# groupby
6573
df = DataFrame(

pandas/tests/generic/test_generic.py

+9
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,12 @@ def test_flags_identity(self, frame_or_series):
451451
assert obj.flags is obj.flags
452452
obj2 = obj.copy()
453453
assert obj2.flags is not obj.flags
454+
455+
def test_bool_dep(self) -> None:
456+
# GH-51749
457+
msg_warn = (
458+
"DataFrame.bool is now deprecated and will be removed "
459+
"in future version of pandas"
460+
)
461+
with tm.assert_produces_warning(FutureWarning, match=msg_warn):
462+
DataFrame({"col": [False]}).bool()

pandas/tests/generic/test_series.py

+36-20
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@ def test_get_bool_data_preserve_dtype(self):
4141

4242
def test_nonzero_single_element(self):
4343
# allow single item via bool method
44+
msg_warn = (
45+
"Series.bool is now deprecated and will be removed "
46+
"in future version of pandas"
47+
)
4448
ser = Series([True])
45-
assert ser.bool()
46-
47-
ser = Series([False])
48-
assert not ser.bool()
49+
ser1 = Series([False])
50+
with tm.assert_produces_warning(FutureWarning, match=msg_warn):
51+
assert ser.bool()
52+
with tm.assert_produces_warning(FutureWarning, match=msg_warn):
53+
assert not ser1.bool()
4954

5055
@pytest.mark.parametrize("data", [np.nan, pd.NaT, True, False])
5156
def test_nonzero_single_element_raise_1(self, data):
@@ -58,35 +63,46 @@ def test_nonzero_single_element_raise_1(self, data):
5863

5964
@pytest.mark.parametrize("data", [np.nan, pd.NaT])
6065
def test_nonzero_single_element_raise_2(self, data):
66+
msg_warn = (
67+
"Series.bool is now deprecated and will be removed "
68+
"in future version of pandas"
69+
)
70+
msg_err = "bool cannot act on a non-boolean single element Series"
6171
series = Series([data])
62-
63-
msg = "bool cannot act on a non-boolean single element Series"
64-
with pytest.raises(ValueError, match=msg):
65-
series.bool()
72+
with tm.assert_produces_warning(FutureWarning, match=msg_warn):
73+
with pytest.raises(ValueError, match=msg_err):
74+
series.bool()
6675

6776
@pytest.mark.parametrize("data", [(True, True), (False, False)])
6877
def test_nonzero_multiple_element_raise(self, data):
6978
# multiple bool are still an error
79+
msg_warn = (
80+
"Series.bool is now deprecated and will be removed "
81+
"in future version of pandas"
82+
)
83+
msg_err = "The truth value of a Series is ambiguous"
7084
series = Series([data])
71-
72-
msg = "The truth value of a Series is ambiguous"
73-
with pytest.raises(ValueError, match=msg):
85+
with pytest.raises(ValueError, match=msg_err):
7486
bool(series)
75-
with pytest.raises(ValueError, match=msg):
76-
series.bool()
87+
with tm.assert_produces_warning(FutureWarning, match=msg_warn):
88+
with pytest.raises(ValueError, match=msg_err):
89+
series.bool()
7790

7891
@pytest.mark.parametrize("data", [1, 0, "a", 0.0])
7992
def test_nonbool_single_element_raise(self, data):
8093
# single non-bool are an error
94+
msg_warn = (
95+
"Series.bool is now deprecated and will be removed "
96+
"in future version of pandas"
97+
)
98+
msg_err1 = "The truth value of a Series is ambiguous"
99+
msg_err2 = "bool cannot act on a non-boolean single element Series"
81100
series = Series([data])
82-
83-
msg = "The truth value of a Series is ambiguous"
84-
with pytest.raises(ValueError, match=msg):
101+
with pytest.raises(ValueError, match=msg_err1):
85102
bool(series)
86-
87-
msg = "bool cannot act on a non-boolean single element Series"
88-
with pytest.raises(ValueError, match=msg):
89-
series.bool()
103+
with tm.assert_produces_warning(FutureWarning, match=msg_warn):
104+
with pytest.raises(ValueError, match=msg_err2):
105+
series.bool()
90106

91107
def test_metadata_propagation_indiv_resample(self):
92108
# resample

0 commit comments

Comments
 (0)