Skip to content

Commit f40fdf1

Browse files
authored
CLN: remove is_categorical_astype (#33211)
1 parent 5df8f82 commit f40fdf1

File tree

2 files changed

+5
-19
lines changed

2 files changed

+5
-19
lines changed

pandas/core/internals/blocks.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
pandas_dtype,
5151
)
5252
from pandas.core.dtypes.concat import concat_categorical, concat_datetime
53-
from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype
53+
from pandas.core.dtypes.dtypes import ExtensionDtype
5454
from pandas.core.dtypes.generic import (
5555
ABCDataFrame,
5656
ABCExtensionArray,
@@ -183,21 +183,6 @@ def is_datelike(self) -> bool:
183183
""" return True if I am a non-datelike """
184184
return self.is_datetime or self.is_timedelta
185185

186-
def is_categorical_astype(self, dtype) -> bool:
187-
"""
188-
validate that we have a astypeable to categorical,
189-
returns a boolean if we are a categorical
190-
"""
191-
if dtype is Categorical or dtype is CategoricalDtype:
192-
# this is a pd.Categorical, but is not
193-
# a valid type for astypeing
194-
raise TypeError(f"invalid type {dtype} for astype")
195-
196-
elif is_categorical_dtype(dtype):
197-
return True
198-
199-
return False
200-
201186
def external_values(self):
202187
"""
203188
The array that Series.values returns (public attribute).
@@ -565,7 +550,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"):
565550
raise TypeError(msg)
566551

567552
# may need to convert to categorical
568-
if self.is_categorical_astype(dtype):
553+
if is_categorical_dtype(dtype):
569554

570555
if is_categorical_dtype(self.values):
571556
# GH 10696/18593: update an existing categorical efficiently

pandas/tests/series/test_dtypes.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,10 @@ def cmp(a, b):
311311

312312
# invalid conversion (these are NOT a dtype)
313313
msg = (
314-
r"invalid type <class 'pandas\.core\.arrays\.categorical\."
315-
"Categorical'> for astype"
314+
"dtype '<class 'pandas.core.arrays.categorical.Categorical'>' "
315+
"not understood"
316316
)
317+
317318
for invalid in [
318319
lambda x: x.astype(Categorical),
319320
lambda x: x.astype("object").astype(Categorical),

0 commit comments

Comments
 (0)