Skip to content

DEPR: deprecate Index.is_boolean #50176

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 2 commits into from
Jan 14, 2023
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ Deprecations
~~~~~~~~~~~~
- Deprecated argument ``infer_datetime_format`` in :func:`to_datetime` and :func:`read_csv`, as a strict version of it is now the default (:issue:`48621`)
- Deprecated :func:`pandas.io.sql.execute`(:issue:`50185`)
- :meth:`Index.is_boolean` has been deprecated. Use :func:`pandas.api.types.is_bool_dtype` instead (:issue:`50042`)
- :meth:`Index.is_integer` has been deprecated. Use :func:`pandas.api.types.is_integer_dtype` instead (:issue:`50042`)
- :meth:`Index.is_floating` has been deprecated. Use :func:`pandas.api.types.is_float_dtype` instead (:issue:`50042`)

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from pandas.core.dtypes.common import (
ensure_int64,
ensure_platform_int,
is_bool_dtype,
is_categorical_dtype,
is_datetime64_dtype,
is_dict_like,
Expand Down Expand Up @@ -600,7 +601,7 @@ def _from_inferred_categories(
cats = to_datetime(inferred_categories, errors="coerce")
elif is_timedelta64_dtype(dtype.categories):
cats = to_timedelta(inferred_categories, errors="coerce")
elif dtype.categories.is_boolean():
elif is_bool_dtype(dtype.categories):
if true_values is None:
true_values = ["True", "TRUE", "true"]

Expand Down
25 changes: 17 additions & 8 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,9 @@ def is_boolean(self) -> bool:
"""
Check if the Index only consists of booleans.

.. deprecated:: 2.0.0
Use `pandas.api.types.is_bool_dtype` instead.

Returns
-------
bool
Expand Down Expand Up @@ -2210,6 +2213,12 @@ def is_boolean(self) -> bool:
>>> idx.is_boolean()
False
"""
warnings.warn(
f"{type(self).__name__}.is_boolean is deprecated. "
"Use pandas.api.types.is_bool_type instead",
FutureWarning,
stacklevel=find_stack_level(),
)
return self.inferred_type in ["boolean"]

@final
Expand All @@ -2227,7 +2236,7 @@ def is_integer(self) -> bool:

See Also
--------
is_boolean : Check if the Index only consists of booleans.
is_boolean : Check if the Index only consists of booleans (deprecated).
is_floating : Check if the Index is a floating type (deprecated).
is_numeric : Check if the Index only consists of numeric data.
is_object : Check if the Index is of the object dtype.
Expand Down Expand Up @@ -2275,7 +2284,7 @@ def is_floating(self) -> bool:

See Also
--------
is_boolean : Check if the Index only consists of booleans.
is_boolean : Check if the Index only consists of booleans (deprecated).
is_integer : Check if the Index only consists of integers (deprecated).
is_numeric : Check if the Index only consists of numeric data.
is_object : Check if the Index is of the object dtype.
Expand Down Expand Up @@ -2320,7 +2329,7 @@ def is_numeric(self) -> bool:

See Also
--------
is_boolean : Check if the Index only consists of booleans.
is_boolean : Check if the Index only consists of booleans (deprecated).
is_integer : Check if the Index only consists of integers (deprecated).
is_floating : Check if the Index is a floating type (deprecated).
is_object : Check if the Index is of the object dtype.
Expand Down Expand Up @@ -2363,7 +2372,7 @@ def is_object(self) -> bool:

See Also
--------
is_boolean : Check if the Index only consists of booleans.
is_boolean : Check if the Index only consists of booleans (deprecated).
is_integer : Check if the Index only consists of integers (deprecated).
is_floating : Check if the Index is a floating type (deprecated).
is_numeric : Check if the Index only consists of numeric data.
Expand Down Expand Up @@ -2404,7 +2413,7 @@ def is_categorical(self) -> bool:
See Also
--------
CategoricalIndex : Index for categorical data.
is_boolean : Check if the Index only consists of booleans.
is_boolean : Check if the Index only consists of booleans (deprecated).
is_integer : Check if the Index only consists of integers (deprecated).
is_floating : Check if the Index is a floating type (deprecated).
is_numeric : Check if the Index only consists of numeric data.
Expand Down Expand Up @@ -2447,7 +2456,7 @@ def is_interval(self) -> bool:
See Also
--------
IntervalIndex : Index for Interval objects.
is_boolean : Check if the Index only consists of booleans.
is_boolean : Check if the Index only consists of booleans (deprecated).
is_integer : Check if the Index only consists of integers (deprecated).
is_floating : Check if the Index is a floating type (deprecated).
is_numeric : Check if the Index only consists of numeric data.
Expand Down Expand Up @@ -5863,8 +5872,8 @@ def _should_compare(self, other: Index) -> bool:
Check if `self == other` can ever have non-False entries.
"""

if (other.is_boolean() and self.is_numeric()) or (
self.is_boolean() and other.is_numeric()
if (is_bool_dtype(other) and self.is_numeric()) or (
is_bool_dtype(self) and other.is_numeric()
):
# GH#16877 Treat boolean labels passed to a numeric index as not
# found. Without this fix False and True would be treated as 0 and 1
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/base/common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Any

from pandas import Index
from pandas.api.types import is_bool_dtype


def allow_na_ops(obj: Any) -> bool:
"""Whether to skip test cases including NaN"""
is_bool_index = isinstance(obj, Index) and obj.is_boolean()
is_bool_index = isinstance(obj, Index) and is_bool_dtype(obj)
return not is_bool_index and obj._can_hold_na
6 changes: 6 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,12 @@ def test_inv(self, simple_index):
with pytest.raises(TypeError, match=msg):
~Series(idx)

def test_is_boolean_is_deprecated(self, simple_index):
# GH50042
idx = simple_index
with tm.assert_produces_warning(FutureWarning):
idx.is_boolean()

def test_is_floating_is_deprecated(self, simple_index):
# GH50042
idx = simple_index
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
import pandas._testing as tm
from pandas.api.types import (
is_bool_dtype,
is_datetime64tz_dtype,
is_signed_integer_dtype,
pandas_dtype,
Expand Down Expand Up @@ -271,7 +272,7 @@ def test_union_base(self, index):
def test_difference_base(self, sort, index):
first = index[2:]
second = index[:4]
if index.is_boolean():
if is_bool_dtype(index):
# i think (TODO: be sure) there assumptions baked in about
# the index fixture that don't hold here?
answer = set(first).difference(set(second))
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
to_timedelta,
)
import pandas._testing as tm
from pandas.api.types import is_scalar
from pandas.api.types import (
is_bool_dtype,
is_scalar,
)
from pandas.core.api import Float64Index
from pandas.core.indexing import _one_ellipsis_message
from pandas.tests.indexing.common import check_indexing_smoketest_or_raises
Expand Down Expand Up @@ -1658,7 +1661,7 @@ def test_loc_iloc_getitem_leading_ellipses(self, series_with_simple_index, index
obj = series_with_simple_index
key = 0 if (indexer is tm.iloc or len(obj) == 0) else obj.index[0]

if indexer is tm.loc and obj.index.is_boolean():
if indexer is tm.loc and is_bool_dtype(obj.index):
# passing [False] will get interpreted as a boolean mask
# TODO: should it? unambiguous when lengths dont match?
return
Expand Down