Skip to content

DEPR: added deprecation warning for NDFrame.bool #51756

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 44 commits into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
abab690
added deprecation warning
ramvikrams Mar 3, 2023
79981ec
Added test and corrected the warning
ramvikrams Mar 3, 2023
ed09b05
Update generic.py
ramvikrams Mar 3, 2023
9972bc6
Update generic.py
ramvikrams Mar 3, 2023
a0dd906
Update test_generic.py
ramvikrams Mar 3, 2023
b0c69d6
removed examples from doc
ramvikrams Mar 4, 2023
e5ccfa4
Update test_generic.py
ramvikrams Mar 4, 2023
4f8814a
Merge branch 'pandas-dev:main' into t03
ramvikrams Mar 10, 2023
748a8e0
added warning in 'ignored_doctest_warnings'
ramvikrams Mar 10, 2023
13cf306
updated path
ramvikrams Mar 10, 2023
70d26ac
updated other tests with bool
ramvikrams Mar 10, 2023
c6bd26f
Update conftest.py
ramvikrams Mar 10, 2023
bcb5bb9
Update test_series.py
ramvikrams Mar 10, 2023
dd2b1e1
required changes done
ramvikrams Mar 14, 2023
ea3fee4
minimal changes
ramvikrams Mar 14, 2023
a84aa47
Update generic.py
ramvikrams Mar 14, 2023
9808540
Update generic.py
ramvikrams Mar 14, 2023
a15a723
Update conftest.py
ramvikrams Mar 14, 2023
ad7c16d
updated the warning
ramvikrams Mar 14, 2023
11fe948
warning message changed
ramvikrams Mar 14, 2023
559520c
Update test_series.py
ramvikrams Mar 14, 2023
9622449
update
ramvikrams Mar 14, 2023
4908a89
Update conftest.py
ramvikrams Mar 14, 2023
f3256db
Update conftest.py
ramvikrams Mar 15, 2023
39f1f63
Merge branch 'main' into t03
ramvikrams Mar 15, 2023
0478351
Update conftest.py
ramvikrams Mar 15, 2023
81882aa
Update conftest.py
ramvikrams Mar 16, 2023
505c82b
Update conftest.py
ramvikrams Mar 16, 2023
2dbdb82
Merge branch 'pandas-dev:main' into t03
ramvikrams Mar 17, 2023
84ab13d
Update conftest.py
ramvikrams Mar 17, 2023
a8c90d5
added okwarning to docs build and whatsnew entry
ramvikrams Mar 23, 2023
73a0fb5
Merge branch 'main' into t03
ramvikrams Mar 23, 2023
baa2b12
merged branches
ramvikrams Mar 23, 2023
f62c490
Update v0.13.0.rst
ramvikrams Mar 23, 2023
b21328f
Update test_frame.py
ramvikrams Mar 23, 2023
712f0f6
Update test_frame.py
ramvikrams Mar 23, 2023
2168bfd
Done required changes
ramvikrams Mar 23, 2023
c3c2c30
Update test_generic.py
ramvikrams Mar 23, 2023
0473b4c
Merge branch 'main' into t03
ramvikrams Mar 23, 2023
5ef2373
Update v0.13.0.rst
ramvikrams Mar 31, 2023
9c518b5
Merge branch 'main' into t03
ramvikrams Mar 31, 2023
1a9b06e
Update v2.1.0.rst
ramvikrams Mar 31, 2023
014d685
Update v0.13.0.rst
ramvikrams Apr 1, 2023
ce4cefa
Merge branch 'main' into t03
ramvikrams Apr 1, 2023
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
10 changes: 0 additions & 10 deletions doc/source/user_guide/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,6 @@ You can test if a pandas object is empty, via the :attr:`~DataFrame.empty` prope
df.empty
pd.DataFrame(columns=list("ABC")).empty
To evaluate single-element pandas objects in a boolean context, use the method
:meth:`~DataFrame.bool`:

.. ipython:: python
pd.Series([True]).bool()
pd.Series([False]).bool()
pd.DataFrame([[True]]).bool()
pd.DataFrame([[False]]).bool()
.. warning::

You might be tempted to do the following:
Expand Down
10 changes: 0 additions & 10 deletions doc/source/user_guide/gotchas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,6 @@ Below is how to check if any of the values are ``True``:
if pd.Series([False, True, False]).any():
print("I am any")
To evaluate single-element pandas objects in a boolean context, use the method
:meth:`~DataFrame.bool`:

.. ipython:: python
pd.Series([True]).bool()
pd.Series([False]).bool()
pd.DataFrame([[True]]).bool()
pd.DataFrame([[False]]).bool()
Bitwise boolean
~~~~~~~~~~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ def pytest_collection_modifyitems(items, config) -> None:
ignored_doctest_warnings = [
# Docstring divides by zero to show behavior difference
("missing.mask_zero_div_zero", "divide by zero encountered"),
(
"pandas.core.generic.NDFrame.bool",
"NDFrame.bool is now deprecated and will be removed in future releases "
"and cases that relied on it will raise in a future version",
),
]

for item in items:
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,13 @@ def bool(self) -> bool_t:
>>> pd.DataFrame({'col': [False]}).bool()
False
"""

warnings.warn(
"NDFrame.bool is now deprecated and will be removed in future releases "
"and cases that relied on it will raise in a future version",
FutureWarning,
stacklevel=find_stack_level(),
)
v = self.squeeze()
if isinstance(v, (bool, np.bool_)):
return bool(v)
Expand Down
29 changes: 17 additions & 12 deletions pandas/tests/generic/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,23 @@ def test_set_axis_name_mi(self, func):

def test_nonzero_single_element(self):
# allow single item via bool method
df = DataFrame([[True]])
assert df.bool()

df = DataFrame([[False]])
assert not df.bool()

df = DataFrame([[False, False]])
msg = "The truth value of a DataFrame is ambiguous"
with pytest.raises(ValueError, match=msg):
df.bool()
with pytest.raises(ValueError, match=msg):
bool(df)
msg = (
"NDFrame.bool is now deprecated and will be removed in future releases "
"and cases that relied on it will raise in a future version"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
df = DataFrame([[True]])
assert df.bool()

df = DataFrame([[False]])
assert not df.bool()

df = DataFrame([[False, False]])
msg = "The truth value of a DataFrame is ambiguous"
with pytest.raises(ValueError, match=msg):
df.bool()
with pytest.raises(ValueError, match=msg):
bool(df)

def test_metadata_propagation_indiv_groupby(self):
# groupby
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,11 @@ def test_flags_identity(self, frame_or_series):
assert obj.flags is obj.flags
obj2 = obj.copy()
assert obj2.flags is not obj.flags

def test_bool_dep(self) -> None:
msg = (
"NDFrame.bool is now deprecated and will be removed in future releases "
"and cases that relied on it will raise in a future version"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
DataFrame({"col": [False]}).bool()
62 changes: 41 additions & 21 deletions pandas/tests/generic/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ def test_get_bool_data_preserve_dtype(self):

def test_nonzero_single_element(self):
# allow single item via bool method
ser = Series([True])
assert ser.bool()
msg = (
"NDFrame.bool is now deprecated and will be removed in future releases "
"and cases that relied on it will raise in a future version"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
ser = Series([True])
assert ser.bool()

ser = Series([False])
assert not ser.bool()
ser = Series([False])
assert not ser.bool()

@pytest.mark.parametrize("data", [np.nan, pd.NaT, True, False])
def test_nonzero_single_element_raise_1(self, data):
Expand All @@ -58,35 +63,50 @@ def test_nonzero_single_element_raise_1(self, data):

@pytest.mark.parametrize("data", [np.nan, pd.NaT])
def test_nonzero_single_element_raise_2(self, data):
series = Series([data])
msg = (
"NDFrame.bool is now deprecated and will be removed in future releases "
"and cases that relied on it will raise in a future version"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
series = Series([data])

msg = "bool cannot act on a non-boolean single element Series"
with pytest.raises(ValueError, match=msg):
series.bool()
msg = "bool cannot act on a non-boolean single element Series"
with pytest.raises(ValueError, match=msg):
series.bool()

@pytest.mark.parametrize("data", [(True, True), (False, False)])
def test_nonzero_multiple_element_raise(self, data):
# multiple bool are still an error
series = Series([data])
msg = (
"NDFrame.bool is now deprecated and will be removed in future releases "
"and cases that relied on it will raise in a future version"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
series = Series([data])

msg = "The truth value of a Series is ambiguous"
with pytest.raises(ValueError, match=msg):
bool(series)
with pytest.raises(ValueError, match=msg):
series.bool()
msg = "The truth value of a Series is ambiguous"
with pytest.raises(ValueError, match=msg):
bool(series)
with pytest.raises(ValueError, match=msg):
series.bool()

@pytest.mark.parametrize("data", [1, 0, "a", 0.0])
def test_nonbool_single_element_raise(self, data):
# single non-bool are an error
series = Series([data])
msg = (
"NDFrame.bool is now deprecated and will be removed in future releases "
"and cases that relied on it will raise in a future version"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
series = Series([data])

msg = "The truth value of a Series is ambiguous"
with pytest.raises(ValueError, match=msg):
bool(series)
msg = "The truth value of a Series is ambiguous"
with pytest.raises(ValueError, match=msg):
bool(series)

msg = "bool cannot act on a non-boolean single element Series"
with pytest.raises(ValueError, match=msg):
series.bool()
msg = "bool cannot act on a non-boolean single element Series"
with pytest.raises(ValueError, match=msg):
series.bool()

def test_metadata_propagation_indiv_resample(self):
# resample
Expand Down
19 changes: 12 additions & 7 deletions pandas/tests/window/test_timeseries_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,18 @@ def test_perf_min(self):
dfp = DataFrame(
{"B": np.random.randn(N)}, index=date_range("20130101", periods=N, freq="s")
)
expected = dfp.rolling(2, min_periods=1).min()
result = dfp.rolling("2s").min()
assert ((result - expected) < 0.01).all().bool()

expected = dfp.rolling(200, min_periods=1).min()
result = dfp.rolling("200s").min()
assert ((result - expected) < 0.01).all().bool()
msg = (
"NDFrame.bool is now deprecated and will be removed in future releases "
"and cases that relied on it will raise in a future version"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
expected = dfp.rolling(2, min_periods=1).min()
result = dfp.rolling("2s").min()
assert ((result - expected) < 0.01).all().bool()

expected = dfp.rolling(200, min_periods=1).min()
result = dfp.rolling("200s").min()
assert ((result - expected) < 0.01).all().bool()

def test_ragged_max(self, ragged):
df = ragged
Expand Down