@@ -575,7 +575,7 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
575
575
# GH 10696/18593/18630
576
576
dtype = self .dtype .update_dtype (dtype )
577
577
self = self .copy () if copy else self
578
- result = self ._set_dtype (dtype )
578
+ result = self ._set_dtype (dtype , copy = False )
579
579
580
580
elif isinstance (dtype , ExtensionDtype ):
581
581
return super ().astype (dtype , copy = copy )
@@ -945,7 +945,7 @@ def _set_categories(self, categories, fastpath: bool = False) -> None:
945
945
946
946
super ().__init__ (self ._ndarray , new_dtype )
947
947
948
- def _set_dtype (self , dtype : CategoricalDtype ) -> Self :
948
+ def _set_dtype (self , dtype : CategoricalDtype , copy : bool = True ) -> Self :
949
949
"""
950
950
Internal method for directly updating the CategoricalDtype
951
951
@@ -958,7 +958,9 @@ def _set_dtype(self, dtype: CategoricalDtype) -> Self:
958
958
We don't do any validation here. It's assumed that the dtype is
959
959
a (valid) instance of `CategoricalDtype`.
960
960
"""
961
- codes = recode_for_categories (self .codes , self .categories , dtype .categories )
961
+ codes = recode_for_categories (
962
+ self .codes , self .categories , dtype .categories , copy
963
+ )
962
964
return type (self )._simple_new (codes , dtype = dtype )
963
965
964
966
def set_ordered (self , value : bool ) -> Self :
0 commit comments