Skip to content

Commit 316c8ba

Browse files
DEPR: deprecate Index.is_categorical (#50225)
* assertion error fix added in pandas/core/indexes/base.py * unrelated changes removed * removing unrelated changes * replaced self with self.dtyp in base.py, removed extra line in 2.0.0.rst * added new line which was removed accidentally * matching warning messege in test * fix for pre-commit check
1 parent a0071f9 commit 316c8ba

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ Deprecations
577577
- :meth:`Index.is_integer` has been deprecated. Use :func:`pandas.api.types.is_integer_dtype` instead (:issue:`50042`)
578578
- :meth:`Index.is_floating` has been deprecated. Use :func:`pandas.api.types.is_float_dtype` instead (:issue:`50042`)
579579
- :meth:`Index.holds_integer` has been deprecated. Use :func:`pandas.api.types.infer_dtype` instead (:issue:`50243`)
580+
- :meth:`Index.is_categorical` has been deprecated. Use :func:`pandas.api.types.is_categorical_dtype` instead (:issue:`50042`)
580581

581582
.. ---------------------------------------------------------------------------
582583
.. _whatsnew_200.prior_deprecations:

pandas/core/indexes/base.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,7 @@ def is_integer(self) -> bool:
22502250
is_floating : Check if the Index is a floating type (deprecated).
22512251
is_numeric : Check if the Index only consists of numeric data.
22522252
is_object : Check if the Index is of the object dtype.
2253-
is_categorical : Check if the Index holds categorical data.
2253+
is_categorical : Check if the Index holds categorical data (deprecated).
22542254
is_interval : Check if the Index holds Interval objects.
22552255
22562256
Examples
@@ -2298,7 +2298,7 @@ def is_floating(self) -> bool:
22982298
is_integer : Check if the Index only consists of integers (deprecated).
22992299
is_numeric : Check if the Index only consists of numeric data.
23002300
is_object : Check if the Index is of the object dtype.
2301-
is_categorical : Check if the Index holds categorical data.
2301+
is_categorical : Check if the Index holds categorical data (deprecated).
23022302
is_interval : Check if the Index holds Interval objects.
23032303
23042304
Examples
@@ -2343,7 +2343,7 @@ def is_numeric(self) -> bool:
23432343
is_integer : Check if the Index only consists of integers (deprecated).
23442344
is_floating : Check if the Index is a floating type (deprecated).
23452345
is_object : Check if the Index is of the object dtype.
2346-
is_categorical : Check if the Index holds categorical data.
2346+
is_categorical : Check if the Index holds categorical data (deprecated).
23472347
is_interval : Check if the Index holds Interval objects.
23482348
23492349
Examples
@@ -2386,7 +2386,7 @@ def is_object(self) -> bool:
23862386
is_integer : Check if the Index only consists of integers (deprecated).
23872387
is_floating : Check if the Index is a floating type (deprecated).
23882388
is_numeric : Check if the Index only consists of numeric data.
2389-
is_categorical : Check if the Index holds categorical data.
2389+
is_categorical : Check if the Index holds categorical data (deprecated).
23902390
is_interval : Check if the Index holds Interval objects.
23912391
23922392
Examples
@@ -2415,6 +2415,9 @@ def is_categorical(self) -> bool:
24152415
"""
24162416
Check if the Index holds categorical data.
24172417
2418+
.. deprecated:: 2.0.0
2419+
Use :meth:`pandas.api.types.is_categorical_dtype` instead.
2420+
24182421
Returns
24192422
-------
24202423
bool
@@ -2451,6 +2454,13 @@ def is_categorical(self) -> bool:
24512454
>>> s.index.is_categorical()
24522455
False
24532456
"""
2457+
warnings.warn(
2458+
f"{type(self).__name__}.is_categorical is deprecated."
2459+
"Use pandas.api.types.is_categorical_dtype instead",
2460+
FutureWarning,
2461+
stacklevel=find_stack_level(),
2462+
)
2463+
24542464
return self.inferred_type in ["categorical"]
24552465

24562466
@final
@@ -2471,7 +2481,7 @@ def is_interval(self) -> bool:
24712481
is_floating : Check if the Index is a floating type (deprecated).
24722482
is_numeric : Check if the Index only consists of numeric data.
24732483
is_object : Check if the Index is of the object dtype.
2474-
is_categorical : Check if the Index holds categorical data.
2484+
is_categorical : Check if the Index holds categorical data (deprecated).
24752485
24762486
Examples
24772487
--------
@@ -5033,7 +5043,11 @@ def _can_hold_identifiers_and_holds_name(self, name) -> bool:
50335043
50345044
https://github.com/pandas-dev/pandas/issues/19764
50355045
"""
5036-
if self.is_object() or is_string_dtype(self.dtype) or self.is_categorical():
5046+
if (
5047+
self.is_object()
5048+
or is_string_dtype(self.dtype)
5049+
or is_categorical_dtype(self.dtype)
5050+
):
50375051
return name in self
50385052
return False
50395053

pandas/tests/indexes/common.py

+9
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,15 @@ def test_holds_integer_deprecated(self, simple_index):
822822
with tm.assert_produces_warning(FutureWarning, match=msg):
823823
idx.holds_integer()
824824

825+
def test_is_categorical_is_deprecated(self, simple_index):
826+
# GH50042
827+
idx = simple_index
828+
with tm.assert_produces_warning(
829+
FutureWarning,
830+
match=r"Use pandas\.api\.types\.is_categorical_dtype instead",
831+
):
832+
idx.is_categorical()
833+
825834

826835
class NumericBase(Base):
827836
"""

0 commit comments

Comments
 (0)