Skip to content

Commit 87a1cc2

Browse files
authored
DEPR: is_categorical (#33385)
1 parent 75fb640 commit 87a1cc2

File tree

8 files changed

+33
-22
lines changed

8 files changed

+33
-22
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ Deprecations
274274
version 1.1. All other arguments should be given as keyword
275275
arguments (:issue:`27573`).
276276

277-
-
277+
- :func:`pandas.api.types.is_categorical` is deprecated and will be removed in a future version; use `:func:pandas.api.types.is_categorical_dtype` instead (:issue:`33385`)
278278

279279
.. ---------------------------------------------------------------------------
280280

pandas/core/dtypes/common.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def ensure_categorical(arr):
125125
cat_arr : The original array cast as a Categorical. If it already
126126
is a Categorical, we return as is.
127127
"""
128-
if not is_categorical(arr):
128+
if not is_categorical_dtype(arr.dtype):
129129
from pandas import Categorical
130130

131131
arr = Categorical(arr)
@@ -360,6 +360,12 @@ def is_categorical(arr) -> bool:
360360
>>> is_categorical(pd.CategoricalIndex([1, 2, 3]))
361361
True
362362
"""
363+
warnings.warn(
364+
"is_categorical is deprecated and will be removed in a future version. "
365+
"Use is_categorical_dtype instead",
366+
FutureWarning,
367+
stacklevel=2,
368+
)
363369
return isinstance(arr, ABCCategorical) or is_categorical_dtype(arr)
364370

365371

@@ -1458,7 +1464,7 @@ def is_extension_type(arr) -> bool:
14581464
stacklevel=2,
14591465
)
14601466

1461-
if is_categorical(arr):
1467+
if is_categorical_dtype(arr):
14621468
return True
14631469
elif is_sparse(arr):
14641470
return True

pandas/core/dtypes/dtypes.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ def _from_values_or_dtype(
337337
>>> pd.CategoricalDtype._from_values_or_dtype(c, dtype=dtype2)
338338
CategoricalDtype(categories=['x', 'y'], ordered=False)
339339
"""
340-
from pandas.core.dtypes.common import is_categorical
341340

342341
if dtype is not None:
343342
# The dtype argument takes precedence over values.dtype (if any)
@@ -352,7 +351,7 @@ def _from_values_or_dtype(
352351
)
353352
elif not isinstance(dtype, CategoricalDtype):
354353
raise ValueError(f"Cannot not construct CategoricalDtype from {dtype}")
355-
elif is_categorical(values):
354+
elif cls.is_dtype(values):
356355
# If no "dtype" was passed, use the one from "values", but honor
357356
# the "ordered" and "categories" arguments
358357
dtype = values.dtype._from_categorical_dtype(

pandas/core/indexes/interval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121
from pandas.core.dtypes.common import (
2222
ensure_platform_int,
23-
is_categorical,
23+
is_categorical_dtype,
2424
is_datetime64tz_dtype,
2525
is_datetime_or_timedelta_dtype,
2626
is_dtype_equal,
@@ -787,7 +787,7 @@ def get_indexer(
787787
left_indexer = self.left.get_indexer(target_as_index.left)
788788
right_indexer = self.right.get_indexer(target_as_index.right)
789789
indexer = np.where(left_indexer == right_indexer, left_indexer, -1)
790-
elif is_categorical(target_as_index):
790+
elif is_categorical_dtype(target_as_index.dtype):
791791
# get an indexer for unique categories then propagate to codes via take_1d
792792
categories_indexer = self.get_indexer(target_as_index.categories)
793793
indexer = take_1d(categories_indexer, target_as_index.codes, fill_value=-1)

pandas/core/internals/blocks.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
DT64NS_DTYPE,
3131
TD64NS_DTYPE,
3232
is_bool_dtype,
33-
is_categorical,
3433
is_categorical_dtype,
3534
is_datetime64_dtype,
3635
is_datetime64tz_dtype,
@@ -2764,7 +2763,7 @@ def get_block_type(values, dtype=None):
27642763
if is_sparse(dtype):
27652764
# Need this first(ish) so that Sparse[datetime] is sparse
27662765
cls = ExtensionBlock
2767-
elif is_categorical(values):
2766+
elif is_categorical_dtype(values.dtype):
27682767
cls = CategoricalBlock
27692768
elif issubclass(vtype, np.datetime64):
27702769
assert not is_datetime64tz_dtype(values)

pandas/core/reshape/merge.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import datetime
77
from functools import partial
88
import string
9-
from typing import TYPE_CHECKING, Optional, Tuple, Union, cast
9+
from typing import TYPE_CHECKING, Optional, Tuple, Union
1010
import warnings
1111

1212
import numpy as np
@@ -24,7 +24,6 @@
2424
is_array_like,
2525
is_bool,
2626
is_bool_dtype,
27-
is_categorical,
2827
is_categorical_dtype,
2928
is_datetime64tz_dtype,
3029
is_dtype_equal,
@@ -1936,9 +1935,8 @@ def _factorize_keys(
19361935
elif (
19371936
is_categorical_dtype(lk) and is_categorical_dtype(rk) and is_dtype_equal(lk, rk)
19381937
):
1939-
assert is_categorical(lk) and is_categorical(rk)
1940-
lk = cast(Categorical, lk)
1941-
rk = cast(Categorical, rk)
1938+
assert isinstance(lk, Categorical)
1939+
assert isinstance(rk, Categorical)
19421940
if lk.categories.equals(rk.categories):
19431941
# if we exactly match in categories, allow us to factorize on codes
19441942
rk = rk.codes

pandas/tests/dtypes/test_common.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,18 @@ def test_is_scipy_sparse():
203203

204204
def test_is_categorical():
205205
cat = pd.Categorical([1, 2, 3])
206-
assert com.is_categorical(cat)
207-
assert com.is_categorical(pd.Series(cat))
208-
assert com.is_categorical(pd.CategoricalIndex([1, 2, 3]))
206+
with tm.assert_produces_warning(FutureWarning):
207+
assert com.is_categorical(cat)
208+
assert com.is_categorical(pd.Series(cat))
209+
assert com.is_categorical(pd.CategoricalIndex([1, 2, 3]))
210+
211+
assert not com.is_categorical([1, 2, 3])
209212

210-
assert not com.is_categorical([1, 2, 3])
213+
214+
def test_is_categorical_deprecation():
215+
# GH#33385
216+
with tm.assert_produces_warning(FutureWarning):
217+
com.is_categorical([1, 2, 3])
211218

212219

213220
def test_is_datetime64_dtype():

pandas/tests/dtypes/test_dtypes.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,12 @@ def test_basic(self, dtype):
159159
assert is_categorical_dtype(s)
160160
assert not is_categorical_dtype(np.dtype("float64"))
161161

162-
assert is_categorical(s.dtype)
163-
assert is_categorical(s)
164-
assert not is_categorical(np.dtype("float64"))
165-
assert not is_categorical(1.0)
162+
with tm.assert_produces_warning(FutureWarning):
163+
# GH#33385 deprecated
164+
assert is_categorical(s.dtype)
165+
assert is_categorical(s)
166+
assert not is_categorical(np.dtype("float64"))
167+
assert not is_categorical(1.0)
166168

167169
def test_tuple_categories(self):
168170
categories = [(1, "a"), (2, "b"), (3, "c")]

0 commit comments

Comments
 (0)