Skip to content

Commit e85edbf

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
DEPR: deprecate Index.is_boolean
1 parent 5fad2e4 commit e85edbf

File tree

7 files changed

+35
-14
lines changed

7 files changed

+35
-14
lines changed

doc/source/whatsnew/v2.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ Other API changes
416416

417417
Deprecations
418418
~~~~~~~~~~~~
419-
-
419+
- :meth:`Index.is_boolean` has been deprecated. Use :func:`pandas.api.types.is_bool_dtype` instead (:issue:`50042`)
420420

421421
.. ---------------------------------------------------------------------------
422422

pandas/core/arrays/categorical.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from pandas.core.dtypes.common import (
4949
ensure_int64,
5050
ensure_platform_int,
51+
is_bool_dtype,
5152
is_categorical_dtype,
5253
is_datetime64_dtype,
5354
is_dict_like,
@@ -605,7 +606,7 @@ def _from_inferred_categories(
605606
cats = to_datetime(inferred_categories, errors="coerce")
606607
elif is_timedelta64_dtype(dtype.categories):
607608
cats = to_timedelta(inferred_categories, errors="coerce")
608-
elif dtype.categories.is_boolean():
609+
elif is_bool_dtype(dtype.categories):
609610
if true_values is None:
610611
true_values = ["True", "TRUE", "true"]
611612

pandas/core/indexes/base.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,9 @@ def is_boolean(self) -> bool:
21932193
"""
21942194
Check if the Index only consists of booleans.
21952195
2196+
.. deprecated:: 2.0.0
2197+
Use `pandas.api.types.is_bool_dtype` instead.
2198+
21962199
Returns
21972200
-------
21982201
bool
@@ -2221,6 +2224,12 @@ def is_boolean(self) -> bool:
22212224
>>> idx.is_boolean()
22222225
False
22232226
"""
2227+
warnings.warn(
2228+
f"{type(self).__name__}.is_boolean is deprecated."
2229+
"Use pandas.api.types.is_bool_type instead",
2230+
FutureWarning,
2231+
stacklevel=find_stack_level(),
2232+
)
22242233
return self.inferred_type in ["boolean"]
22252234

22262235
@final
@@ -2235,7 +2244,7 @@ def is_integer(self) -> bool:
22352244
22362245
See Also
22372246
--------
2238-
is_boolean : Check if the Index only consists of booleans.
2247+
is_boolean : Check if the Index only consists of booleans (deprecated).
22392248
is_floating : Check if the Index is a floating type.
22402249
is_numeric : Check if the Index only consists of numeric data.
22412250
is_object : Check if the Index is of the object dtype.
@@ -2274,7 +2283,7 @@ def is_floating(self) -> bool:
22742283
22752284
See Also
22762285
--------
2277-
is_boolean : Check if the Index only consists of booleans.
2286+
is_boolean : Check if the Index only consists of booleans (deprecated).
22782287
is_integer : Check if the Index only consists of integers.
22792288
is_numeric : Check if the Index only consists of numeric data.
22802289
is_object : Check if the Index is of the object dtype.
@@ -2313,7 +2322,7 @@ def is_numeric(self) -> bool:
23132322
23142323
See Also
23152324
--------
2316-
is_boolean : Check if the Index only consists of booleans.
2325+
is_boolean : Check if the Index only consists of booleans (deprecated).
23172326
is_integer : Check if the Index only consists of integers.
23182327
is_floating : Check if the Index is a floating type.
23192328
is_object : Check if the Index is of the object dtype.
@@ -2356,7 +2365,7 @@ def is_object(self) -> bool:
23562365
23572366
See Also
23582367
--------
2359-
is_boolean : Check if the Index only consists of booleans.
2368+
is_boolean : Check if the Index only consists of booleans (deprecated).
23602369
is_integer : Check if the Index only consists of integers.
23612370
is_floating : Check if the Index is a floating type.
23622371
is_numeric : Check if the Index only consists of numeric data.
@@ -2397,7 +2406,7 @@ def is_categorical(self) -> bool:
23972406
See Also
23982407
--------
23992408
CategoricalIndex : Index for categorical data.
2400-
is_boolean : Check if the Index only consists of booleans.
2409+
is_boolean : Check if the Index only consists of booleans (deprecated).
24012410
is_integer : Check if the Index only consists of integers.
24022411
is_floating : Check if the Index is a floating type.
24032412
is_numeric : Check if the Index only consists of numeric data.
@@ -2440,7 +2449,7 @@ def is_interval(self) -> bool:
24402449
See Also
24412450
--------
24422451
IntervalIndex : Index for Interval objects.
2443-
is_boolean : Check if the Index only consists of booleans.
2452+
is_boolean : Check if the Index only consists of booleans (deprecated).
24442453
is_integer : Check if the Index only consists of integers.
24452454
is_floating : Check if the Index is a floating type.
24462455
is_numeric : Check if the Index only consists of numeric data.
@@ -5855,8 +5864,8 @@ def _should_compare(self, other: Index) -> bool:
58555864
Check if `self == other` can ever have non-False entries.
58565865
"""
58575866

5858-
if (other.is_boolean() and self.is_numeric()) or (
5859-
self.is_boolean() and other.is_numeric()
5867+
if (is_bool_dtype(other) and self.is_numeric()) or (
5868+
is_bool_dtype(self) and other.is_numeric()
58605869
):
58615870
# GH#16877 Treat boolean labels passed to a numeric index as not
58625871
# found. Without this fix False and True would be treated as 0 and 1

pandas/tests/base/common.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import Any
22

33
from pandas import Index
4+
from pandas.api.types import is_bool_dtype
45

56

67
def allow_na_ops(obj: Any) -> bool:
78
"""Whether to skip test cases including NaN"""
8-
is_bool_index = isinstance(obj, Index) and obj.is_boolean()
9+
is_bool_index = isinstance(obj, Index) and is_bool_dtype(obj)
910
return not is_bool_index and obj._can_hold_na

pandas/tests/indexes/common.py

+6
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,12 @@ def test_inv(self, simple_index):
795795
with pytest.raises(TypeError, match=msg):
796796
~Series(idx)
797797

798+
def test_is_boolean_is_deprecated(self, simple_index):
799+
# GH50042
800+
idx = simple_index
801+
with tm.assert_produces_warning(FutureWarning):
802+
idx.is_boolean()
803+
798804

799805
class NumericBase(Base):
800806
"""

pandas/tests/indexes/test_setops.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
import pandas._testing as tm
2727
from pandas.api.types import (
28+
is_bool_dtype,
2829
is_datetime64tz_dtype,
2930
is_signed_integer_dtype,
3031
pandas_dtype,
@@ -271,7 +272,7 @@ def test_union_base(self, index):
271272
def test_difference_base(self, sort, index):
272273
first = index[2:]
273274
second = index[:4]
274-
if index.is_boolean():
275+
if is_bool_dtype(index):
275276
# i think (TODO: be sure) there assumptions baked in about
276277
# the index fixture that don't hold here?
277278
answer = set(first).difference(set(second))

pandas/tests/indexing/test_loc.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
to_timedelta,
3838
)
3939
import pandas._testing as tm
40-
from pandas.api.types import is_scalar
40+
from pandas.api.types import (
41+
is_bool_dtype,
42+
is_scalar,
43+
)
4144
from pandas.core.api import Float64Index
4245
from pandas.core.indexing import _one_ellipsis_message
4346
from pandas.tests.indexing.common import check_indexing_smoketest_or_raises
@@ -1668,7 +1671,7 @@ def test_loc_iloc_getitem_leading_ellipses(self, series_with_simple_index, index
16681671
obj = series_with_simple_index
16691672
key = 0 if (indexer is tm.iloc or len(obj) == 0) else obj.index[0]
16701673

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

0 commit comments

Comments
 (0)