diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 778e2866f1eff..bdfb44cdc2fa3 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -50,7 +50,7 @@ pandas_dtype, ) from pandas.core.dtypes.concat import concat_categorical, concat_datetime -from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype +from pandas.core.dtypes.dtypes import ExtensionDtype from pandas.core.dtypes.generic import ( ABCDataFrame, ABCExtensionArray, @@ -183,21 +183,6 @@ def is_datelike(self) -> bool: """ return True if I am a non-datelike """ return self.is_datetime or self.is_timedelta - def is_categorical_astype(self, dtype) -> bool: - """ - validate that we have a astypeable to categorical, - returns a boolean if we are a categorical - """ - if dtype is Categorical or dtype is CategoricalDtype: - # this is a pd.Categorical, but is not - # a valid type for astypeing - raise TypeError(f"invalid type {dtype} for astype") - - elif is_categorical_dtype(dtype): - return True - - return False - def external_values(self): """ The array that Series.values returns (public attribute). @@ -565,7 +550,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"): raise TypeError(msg) # may need to convert to categorical - if self.is_categorical_astype(dtype): + if is_categorical_dtype(dtype): if is_categorical_dtype(self.values): # GH 10696/18593: update an existing categorical efficiently diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index 31f17be2fac7b..2f2a663d559d0 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -311,9 +311,10 @@ def cmp(a, b): # invalid conversion (these are NOT a dtype) msg = ( - r"invalid type for astype" + "dtype '' " + "not understood" ) + for invalid in [ lambda x: x.astype(Categorical), lambda x: x.astype("object").astype(Categorical),