@@ -579,11 +579,12 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
579579 raise ValueError ("Cannot convert float NaN to integer" )
580580
581581 elif len (self .codes ) == 0 or len (self .categories ) == 0 :
582- result = np .array (
583- self ,
584- dtype = dtype ,
585- copy = copy ,
586- )
582+ # For NumPy 1.x compatibility we cannot use copy=None. And
583+ # `copy=False` has the meaning of `copy=None` here:
584+ if not copy :
585+ result = np .asarray (self , dtype = dtype )
586+ else :
587+ result = np .array (self , dtype = dtype )
587588
588589 else :
589590 # GH8628 (PERF): astype category codes instead of astyping array
@@ -1663,7 +1664,7 @@ def __array__(
16631664 Specifies the the dtype for the array.
16641665
16651666 copy : bool or None, optional
1666- Unused .
1667+ See :func:`numpy.asarray` .
16671668
16681669 Returns
16691670 -------
@@ -1686,13 +1687,18 @@ def __array__(
16861687 >>> np.asarray(cat)
16871688 array(['a', 'b'], dtype=object)
16881689 """
1690+ if copy is False :
1691+ raise ValueError (
1692+ "Unable to avoid copy while creating an array as requested."
1693+ )
1694+
16891695 ret = take_nd (self .categories ._values , self ._codes )
1690- if dtype and np .dtype (dtype ) != self .categories .dtype :
1691- return np .asarray (ret , dtype )
16921696 # When we're a Categorical[ExtensionArray], like Interval,
16931697 # we need to ensure __array__ gets all the way to an
16941698 # ndarray.
1695- return np .asarray (ret )
1699+
1700+ # `take_nd` should already make a copy, so don't force again.
1701+ return np .asarray (ret , dtype = dtype )
16961702
16971703 def __array_ufunc__ (self , ufunc : np .ufunc , method : str , * inputs , ** kwargs ):
16981704 # for binary ops, use our custom dunder methods
0 commit comments