Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: is_categorical #33385

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Deprecations
version 1.1. All other arguments should be given as keyword
arguments (:issue:`27573`).

-
- :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`)

.. ---------------------------------------------------------------------------

Expand Down
10 changes: 8 additions & 2 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def ensure_categorical(arr):
cat_arr : The original array cast as a Categorical. If it already
is a Categorical, we return as is.
"""
if not is_categorical(arr):
if not is_categorical_dtype(arr.dtype):
from pandas import Categorical

arr = Categorical(arr)
Expand Down Expand Up @@ -360,6 +360,12 @@ def is_categorical(arr) -> bool:
>>> is_categorical(pd.CategoricalIndex([1, 2, 3]))
True
"""
warnings.warn(
"is_categorical is deprecated and will be removed in a future version. "
"Use is_categorical_dtype instead",
FutureWarning,
stacklevel=2,
)
return isinstance(arr, ABCCategorical) or is_categorical_dtype(arr)


Expand Down Expand Up @@ -1458,7 +1464,7 @@ def is_extension_type(arr) -> bool:
stacklevel=2,
)

if is_categorical(arr):
if is_categorical_dtype(arr):
return True
elif is_sparse(arr):
return True
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ def _from_values_or_dtype(
>>> pd.CategoricalDtype._from_values_or_dtype(c, dtype=dtype2)
CategoricalDtype(categories=['x', 'y'], ordered=False)
"""
from pandas.core.dtypes.common import is_categorical

if dtype is not None:
# The dtype argument takes precedence over values.dtype (if any)
Expand All @@ -352,7 +351,7 @@ def _from_values_or_dtype(
)
elif not isinstance(dtype, CategoricalDtype):
raise ValueError(f"Cannot not construct CategoricalDtype from {dtype}")
elif is_categorical(values):
elif cls.is_dtype(values):
# If no "dtype" was passed, use the one from "values", but honor
# the "ordered" and "categories" arguments
dtype = values.dtype._from_categorical_dtype(
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from pandas.core.dtypes.common import (
ensure_platform_int,
is_categorical,
is_categorical_dtype,
is_datetime64tz_dtype,
is_datetime_or_timedelta_dtype,
is_dtype_equal,
Expand Down Expand Up @@ -787,7 +787,7 @@ def get_indexer(
left_indexer = self.left.get_indexer(target_as_index.left)
right_indexer = self.right.get_indexer(target_as_index.right)
indexer = np.where(left_indexer == right_indexer, left_indexer, -1)
elif is_categorical(target_as_index):
elif is_categorical_dtype(target_as_index.dtype):
# get an indexer for unique categories then propagate to codes via take_1d
categories_indexer = self.get_indexer(target_as_index.categories)
indexer = take_1d(categories_indexer, target_as_index.codes, fill_value=-1)
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
DT64NS_DTYPE,
TD64NS_DTYPE,
is_bool_dtype,
is_categorical,
is_categorical_dtype,
is_datetime64_dtype,
is_datetime64tz_dtype,
Expand Down Expand Up @@ -2764,7 +2763,7 @@ def get_block_type(values, dtype=None):
if is_sparse(dtype):
# Need this first(ish) so that Sparse[datetime] is sparse
cls = ExtensionBlock
elif is_categorical(values):
elif is_categorical_dtype(values.dtype):
cls = CategoricalBlock
elif issubclass(vtype, np.datetime64):
assert not is_datetime64tz_dtype(values)
Expand Down
8 changes: 3 additions & 5 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import datetime
from functools import partial
import string
from typing import TYPE_CHECKING, Optional, Tuple, Union, cast
from typing import TYPE_CHECKING, Optional, Tuple, Union
import warnings

import numpy as np
Expand All @@ -24,7 +24,6 @@
is_array_like,
is_bool,
is_bool_dtype,
is_categorical,
is_categorical_dtype,
is_datetime64tz_dtype,
is_dtype_equal,
Expand Down Expand Up @@ -1936,9 +1935,8 @@ def _factorize_keys(
elif (
is_categorical_dtype(lk) and is_categorical_dtype(rk) and is_dtype_equal(lk, rk)
):
assert is_categorical(lk) and is_categorical(rk)
lk = cast(Categorical, lk)
rk = cast(Categorical, rk)
assert isinstance(lk, Categorical)
assert isinstance(rk, Categorical)
if lk.categories.equals(rk.categories):
# if we exactly match in categories, allow us to factorize on codes
rk = rk.codes
Expand Down
15 changes: 11 additions & 4 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,18 @@ def test_is_scipy_sparse():

def test_is_categorical():
cat = pd.Categorical([1, 2, 3])
assert com.is_categorical(cat)
assert com.is_categorical(pd.Series(cat))
assert com.is_categorical(pd.CategoricalIndex([1, 2, 3]))
with tm.assert_produces_warning(FutureWarning):
assert com.is_categorical(cat)
assert com.is_categorical(pd.Series(cat))
assert com.is_categorical(pd.CategoricalIndex([1, 2, 3]))

assert not com.is_categorical([1, 2, 3])

assert not com.is_categorical([1, 2, 3])

def test_is_categorical_deprecation():
# GH#33385
with tm.assert_produces_warning(FutureWarning):
com.is_categorical([1, 2, 3])


def test_is_datetime64_dtype():
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,12 @@ def test_basic(self, dtype):
assert is_categorical_dtype(s)
assert not is_categorical_dtype(np.dtype("float64"))

assert is_categorical(s.dtype)
assert is_categorical(s)
assert not is_categorical(np.dtype("float64"))
assert not is_categorical(1.0)
with tm.assert_produces_warning(FutureWarning):
# GH#33385 deprecated
assert is_categorical(s.dtype)
assert is_categorical(s)
assert not is_categorical(np.dtype("float64"))
assert not is_categorical(1.0)

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