11from shutil import get_terminal_size
22import textwrap
3+ from typing import Type , Union , cast
34from warnings import warn
45
56import numpy as np
4748from pandas .core .dtypes .inference import is_hashable
4849from pandas .core .dtypes .missing import isna , notna
4950
51+ from pandas ._typing import ArrayLike , Dtype , OrderedType
5052from pandas .core import ops
5153from pandas .core .accessor import PandasDelegate , delegate_names
5254import pandas .core .algorithms as algorithms
@@ -466,7 +468,7 @@ def categories(self, categories):
466468 self ._dtype = new_dtype
467469
468470 @property
469- def ordered (self ):
471+ def ordered (self ) -> OrderedType :
470472 """
471473 Whether the categories have an ordered relationship.
472474 """
@@ -480,11 +482,11 @@ def dtype(self) -> CategoricalDtype:
480482 return self ._dtype
481483
482484 @property
483- def _ndarray_values (self ):
485+ def _ndarray_values (self ) -> np . ndarray :
484486 return self .codes
485487
486488 @property
487- def _constructor (self ):
489+ def _constructor (self ) -> Type [ "Categorical" ] :
488490 return Categorical
489491
490492 @classmethod
@@ -495,15 +497,15 @@ def _formatter(self, boxed=False):
495497 # Defer to CategoricalFormatter's formatter.
496498 return None
497499
498- def copy (self ):
500+ def copy (self ) -> "Categorical" :
499501 """
500502 Copy constructor.
501503 """
502504 return self ._constructor (
503505 values = self ._codes .copy (), dtype = self .dtype , fastpath = True
504506 )
505507
506- def astype (self , dtype , copy = True ):
508+ def astype (self , dtype : Dtype , copy : bool = True ) -> ArrayLike :
507509 """
508510 Coerce this type to another dtype
509511
@@ -519,6 +521,8 @@ def astype(self, dtype, copy=True):
519521
520522 """
521523 if is_categorical_dtype (dtype ):
524+ dtype = cast (Union [str , CategoricalDtype ], dtype )
525+
522526 # GH 10696/18593
523527 dtype = self .dtype .update_dtype (dtype )
524528 self = self .copy () if copy else self
@@ -528,27 +532,27 @@ def astype(self, dtype, copy=True):
528532 return np .array (self , dtype = dtype , copy = copy )
529533
530534 @cache_readonly
531- def ndim (self ):
535+ def ndim (self ) -> int :
532536 """
533537 Number of dimensions of the Categorical
534538 """
535539 return self ._codes .ndim
536540
537541 @cache_readonly
538- def size (self ):
542+ def size (self ) -> int :
539543 """
540544 return the len of myself
541545 """
542546 return len (self )
543547
544548 @cache_readonly
545- def itemsize (self ):
549+ def itemsize (self ) -> int :
546550 """
547551 return the size of a single category
548552 """
549553 return self .categories .itemsize
550554
551- def tolist (self ):
555+ def tolist (self ) -> list :
552556 """
553557 Return a list of the values.
554558
@@ -561,7 +565,7 @@ def tolist(self):
561565 to_list = tolist
562566
563567 @property
564- def base (self ):
568+ def base (self ) -> None :
565569 """
566570 compat, we are always our own object
567571 """
@@ -769,7 +773,7 @@ def _set_categories(self, categories, fastpath=False):
769773
770774 self ._dtype = new_dtype
771775
772- def _set_dtype (self , dtype ) :
776+ def _set_dtype (self , dtype : CategoricalDtype ) -> "Categorical" :
773777 """
774778 Internal method for directly updating the CategoricalDtype
775779
0 commit comments