@@ -581,11 +581,10 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
581581 elif len (self .codes ) == 0 or len (self .categories ) == 0 :
582582 # For NumPy 1.x compatibility we cannot use copy=None. And
583583 # `copy=False` has the meaning of `copy=None` here:
584- asarray_func = np .array if copy else np .asarray
585- result = asarray_func (
586- self ,
587- dtype = dtype ,
588- )
584+ if not copy :
585+ result = np .asarray (self , dtype = dtype )
586+ else :
587+ result = np .array (self , dtype = dtype )
589588
590589 else :
591590 # GH8628 (PERF): astype category codes instead of astyping array
@@ -1693,16 +1692,15 @@ def __array__(
16931692 "Unable to avoid copy while creating an array as requested."
16941693 )
16951694
1696- # TODO: using asarray_func because NumPy 1.x doesn't support copy=None
1697- asarray_func = np .asarray if copy is None else np .array
1698-
16991695 ret = take_nd (self .categories ._values , self ._codes )
1700- if dtype and np .dtype (dtype ) != self .categories .dtype :
1701- return asarray_func (ret , dtype )
17021696 # When we're a Categorical[ExtensionArray], like Interval,
17031697 # we need to ensure __array__ gets all the way to an
17041698 # ndarray.
1705- return asarray_func (ret )
1699+
1700+ if copy is None :
1701+ # Branch required since copy=None is not defined on 1.x
1702+ return np .asarray (ret , dtype = dtype )
1703+ return np .array (ret , dtype = dtype )
17061704
17071705 def __array_ufunc__ (self , ufunc : np .ufunc , method : str , * inputs , ** kwargs ):
17081706 # for binary ops, use our custom dunder methods
0 commit comments