diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 033f47f0c994d..7f983c97691ca 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -577,6 +577,7 @@ Deprecations - :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`) - :meth:`Index.holds_integer` has been deprecated. Use :func:`pandas.api.types.infer_dtype` instead (:issue:`50243`) +- :meth:`Index.is_categorical` has been deprecated. Use :func:`pandas.api.types.is_categorical_dtype` instead (:issue:`50042`) .. --------------------------------------------------------------------------- .. _whatsnew_200.prior_deprecations: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 7d5a7ac5945d6..9f86e5276b0d6 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2250,7 +2250,7 @@ def is_integer(self) -> bool: 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. - is_categorical : Check if the Index holds categorical data. + is_categorical : Check if the Index holds categorical data (deprecated). is_interval : Check if the Index holds Interval objects. Examples @@ -2298,7 +2298,7 @@ def is_floating(self) -> bool: 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. - is_categorical : Check if the Index holds categorical data. + is_categorical : Check if the Index holds categorical data (deprecated). is_interval : Check if the Index holds Interval objects. Examples @@ -2343,7 +2343,7 @@ def is_numeric(self) -> bool: 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. - is_categorical : Check if the Index holds categorical data. + is_categorical : Check if the Index holds categorical data (deprecated). is_interval : Check if the Index holds Interval objects. Examples @@ -2386,7 +2386,7 @@ def is_object(self) -> bool: 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. - is_categorical : Check if the Index holds categorical data. + is_categorical : Check if the Index holds categorical data (deprecated). is_interval : Check if the Index holds Interval objects. Examples @@ -2415,6 +2415,9 @@ def is_categorical(self) -> bool: """ Check if the Index holds categorical data. + .. deprecated:: 2.0.0 + Use :meth:`pandas.api.types.is_categorical_dtype` instead. + Returns ------- bool @@ -2451,6 +2454,13 @@ def is_categorical(self) -> bool: >>> s.index.is_categorical() False """ + warnings.warn( + f"{type(self).__name__}.is_categorical is deprecated." + "Use pandas.api.types.is_categorical_dtype instead", + FutureWarning, + stacklevel=find_stack_level(), + ) + return self.inferred_type in ["categorical"] @final @@ -2471,7 +2481,7 @@ def is_interval(self) -> bool: 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. - is_categorical : Check if the Index holds categorical data. + is_categorical : Check if the Index holds categorical data (deprecated). Examples -------- @@ -5033,7 +5043,11 @@ def _can_hold_identifiers_and_holds_name(self, name) -> bool: https://github.com/pandas-dev/pandas/issues/19764 """ - if self.is_object() or is_string_dtype(self.dtype) or self.is_categorical(): + if ( + self.is_object() + or is_string_dtype(self.dtype) + or is_categorical_dtype(self.dtype) + ): return name in self return False diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index ed8eb350234d1..779c1cc6ccd05 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -822,6 +822,15 @@ def test_holds_integer_deprecated(self, simple_index): with tm.assert_produces_warning(FutureWarning, match=msg): idx.holds_integer() + def test_is_categorical_is_deprecated(self, simple_index): + # GH50042 + idx = simple_index + with tm.assert_produces_warning( + FutureWarning, + match=r"Use pandas\.api\.types\.is_categorical_dtype instead", + ): + idx.is_categorical() + class NumericBase(Base): """