Skip to content

CLN: remove Block.is_categorical_astype #33211

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/series/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,10 @@ def cmp(a, b):

# invalid conversion (these are NOT a dtype)
msg = (
r"invalid type <class 'pandas\.core\.arrays\.categorical\."
"Categorical'> for astype"
"dtype '<class 'pandas.core.arrays.categorical.Categorical'>' "
"not understood"
)

for invalid in [
lambda x: x.astype(Categorical),
lambda x: x.astype("object").astype(Categorical),
Expand Down