Skip to content

Commit 596ee34

Browse files
Terji Petersentopper-123
Terji Petersen
authored andcommitted
DEPR: deprecate Index.is_boolean
1 parent f15a65c commit 596ee34

File tree

7 files changed

+35
-13
lines changed

7 files changed

+35
-13
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ Deprecations
569569
~~~~~~~~~~~~
570570
- 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`)
571571
- Deprecated :func:`pandas.io.sql.execute`(:issue:`50185`)
572+
- :meth:`Index.is_boolean` has been deprecated. Use :func:`pandas.api.types.is_bool_dtype` instead (:issue:`50042`)
572573
- :meth:`Index.is_integer` has been deprecated. Use :func:`pandas.api.types.is_integer_dtype` instead (:issue:`50042`)
573574
- :meth:`Index.is_floating` has been deprecated. Use :func:`pandas.api.types.is_float_dtype` instead (:issue:`50042`)
574575

pandas/core/arrays/categorical.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from pandas.core.dtypes.common import (
4848
ensure_int64,
4949
ensure_platform_int,
50+
is_bool_dtype,
5051
is_categorical_dtype,
5152
is_datetime64_dtype,
5253
is_dict_like,
@@ -600,7 +601,7 @@ def _from_inferred_categories(
600601
cats = to_datetime(inferred_categories, errors="coerce")
601602
elif is_timedelta64_dtype(dtype.categories):
602603
cats = to_timedelta(inferred_categories, errors="coerce")
603-
elif dtype.categories.is_boolean():
604+
elif is_bool_dtype(dtype.categories):
604605
if true_values is None:
605606
true_values = ["True", "TRUE", "true"]
606607

pandas/core/indexes/base.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,9 @@ def is_boolean(self) -> bool:
21822182
"""
21832183
Check if the Index only consists of booleans.
21842184
2185+
.. deprecated:: 2.0.0
2186+
Use `pandas.api.types.is_bool_dtype` instead.
2187+
21852188
Returns
21862189
-------
21872190
bool
@@ -2210,6 +2213,12 @@ def is_boolean(self) -> bool:
22102213
>>> idx.is_boolean()
22112214
False
22122215
"""
2216+
warnings.warn(
2217+
f"{type(self).__name__}.is_boolean is deprecated."
2218+
"Use pandas.api.types.is_bool_type instead",
2219+
FutureWarning,
2220+
stacklevel=find_stack_level(),
2221+
)
22132222
return self.inferred_type in ["boolean"]
22142223

22152224
@final
@@ -2227,7 +2236,7 @@ def is_integer(self) -> bool:
22272236
22282237
See Also
22292238
--------
2230-
is_boolean : Check if the Index only consists of booleans.
2239+
is_boolean : Check if the Index only consists of booleans (deprecated).
22312240
is_floating : Check if the Index is a floating type (deprecated).
22322241
is_numeric : Check if the Index only consists of numeric data.
22332242
is_object : Check if the Index is of the object dtype.
@@ -2275,7 +2284,7 @@ def is_floating(self) -> bool:
22752284
22762285
See Also
22772286
--------
2278-
is_boolean : Check if the Index only consists of booleans.
2287+
is_boolean : Check if the Index only consists of booleans (deprecated).
22792288
is_integer : Check if the Index only consists of integers (deprecated).
22802289
is_numeric : Check if the Index only consists of numeric data.
22812290
is_object : Check if the Index is of the object dtype.
@@ -2320,7 +2329,7 @@ def is_numeric(self) -> bool:
23202329
23212330
See Also
23222331
--------
2323-
is_boolean : Check if the Index only consists of booleans.
2332+
is_boolean : Check if the Index only consists of booleans (deprecated).
23242333
is_integer : Check if the Index only consists of integers (deprecated).
23252334
is_floating : Check if the Index is a floating type (deprecated).
23262335
is_object : Check if the Index is of the object dtype.
@@ -2363,7 +2372,7 @@ def is_object(self) -> bool:
23632372
23642373
See Also
23652374
--------
2366-
is_boolean : Check if the Index only consists of booleans.
2375+
is_boolean : Check if the Index only consists of booleans (deprecated).
23672376
is_integer : Check if the Index only consists of integers (deprecated).
23682377
is_floating : Check if the Index is a floating type (deprecated).
23692378
is_numeric : Check if the Index only consists of numeric data.
@@ -2404,7 +2413,7 @@ def is_categorical(self) -> bool:
24042413
See Also
24052414
--------
24062415
CategoricalIndex : Index for categorical data.
2407-
is_boolean : Check if the Index only consists of booleans.
2416+
is_boolean : Check if the Index only consists of booleans (deprecated).
24082417
is_integer : Check if the Index only consists of integers (deprecated).
24092418
is_floating : Check if the Index is a floating type (deprecated).
24102419
is_numeric : Check if the Index only consists of numeric data.
@@ -2447,7 +2456,7 @@ def is_interval(self) -> bool:
24472456
See Also
24482457
--------
24492458
IntervalIndex : Index for Interval objects.
2450-
is_boolean : Check if the Index only consists of booleans.
2459+
is_boolean : Check if the Index only consists of booleans (deprecated).
24512460
is_integer : Check if the Index only consists of integers (deprecated).
24522461
is_floating : Check if the Index is a floating type (deprecated).
24532462
is_numeric : Check if the Index only consists of numeric data.
@@ -5863,8 +5872,8 @@ def _should_compare(self, other: Index) -> bool:
58635872
Check if `self == other` can ever have non-False entries.
58645873
"""
58655874

5866-
if (other.is_boolean() and self.is_numeric()) or (
5867-
self.is_boolean() and other.is_numeric()
5875+
if (is_bool_dtype(other) and self.is_numeric()) or (
5876+
is_bool_dtype(self) and other.is_numeric()
58685877
):
58695878
# GH#16877 Treat boolean labels passed to a numeric index as not
58705879
# 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
@@ -797,6 +797,12 @@ def test_inv(self, simple_index):
797797
with pytest.raises(TypeError, match=msg):
798798
~Series(idx)
799799

800+
def test_is_boolean_is_deprecated(self, simple_index):
801+
# GH50042
802+
idx = simple_index
803+
with tm.assert_produces_warning(FutureWarning):
804+
idx.is_boolean()
805+
800806
def test_is_floating_is_deprecated(self, simple_index):
801807
# GH50042
802808
idx = simple_index

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
@@ -1658,7 +1661,7 @@ def test_loc_iloc_getitem_leading_ellipses(self, series_with_simple_index, index
16581661
obj = series_with_simple_index
16591662
key = 0 if (indexer is tm.iloc or len(obj) == 0) else obj.index[0]
16601663

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

0 commit comments

Comments
 (0)