Skip to content

Fix API: astype method fails to raise errors for category data type #59963

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

Closed
wants to merge 13 commits into from
14 changes: 13 additions & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,9 @@ def astype(self, dtype: ExtensionDtype, copy: bool = ...) -> ExtensionArray: ...
@overload
def astype(self, dtype: AstypeArg, copy: bool = ...) -> ArrayLike: ...

def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
def astype(
self, dtype: AstypeArg, copy: bool = True, errors: str = "raise"
) -> ArrayLike:
"""
Coerce this type to another dtype

Expand All @@ -560,6 +562,11 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
By default, astype always returns a newly allocated object.
If copy is set to False and dtype is categorical, the original
object is returned.
errors : {'raise', 'ignore'}, default 'raise'
Control raising of exceptions on invalid data for provided dtype.

- 'raise' : allow exceptions to be raised
- 'ignore' : suppress exceptions. On error return original object
"""
dtype = pandas_dtype(dtype)
result: Categorical | np.ndarray
Expand Down Expand Up @@ -603,6 +610,11 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
msg = f"Cannot cast {self.categories.dtype} dtype to {dtype}"
raise ValueError(msg) from err

if errors == "raise" and not np.all(np.isin(self.codes, new_cats)):
raise ValueError(
"Cannot convert to CategoricalDtype with undefined values"
)

result = take_nd(
new_cats, ensure_platform_int(self._codes), fill_value=fill_value
)
Expand Down
Loading