1
1
from shutil import get_terminal_size
2
2
import textwrap
3
+ from typing import Type , Union , cast
3
4
from warnings import warn
4
5
5
6
import numpy as np
47
48
from pandas .core .dtypes .inference import is_hashable
48
49
from pandas .core .dtypes .missing import isna , notna
49
50
51
+ from pandas ._typing import ArrayLike , Dtype , OrderedType
50
52
from pandas .core import ops
51
53
from pandas .core .accessor import PandasDelegate , delegate_names
52
54
import pandas .core .algorithms as algorithms
@@ -466,7 +468,7 @@ def categories(self, categories):
466
468
self ._dtype = new_dtype
467
469
468
470
@property
469
- def ordered (self ):
471
+ def ordered (self ) -> OrderedType :
470
472
"""
471
473
Whether the categories have an ordered relationship.
472
474
"""
@@ -480,11 +482,11 @@ def dtype(self) -> CategoricalDtype:
480
482
return self ._dtype
481
483
482
484
@property
483
- def _ndarray_values (self ):
485
+ def _ndarray_values (self ) -> np . ndarray :
484
486
return self .codes
485
487
486
488
@property
487
- def _constructor (self ):
489
+ def _constructor (self ) -> Type [ "Categorical" ] :
488
490
return Categorical
489
491
490
492
@classmethod
@@ -495,15 +497,15 @@ def _formatter(self, boxed=False):
495
497
# Defer to CategoricalFormatter's formatter.
496
498
return None
497
499
498
- def copy (self ):
500
+ def copy (self ) -> "Categorical" :
499
501
"""
500
502
Copy constructor.
501
503
"""
502
504
return self ._constructor (
503
505
values = self ._codes .copy (), dtype = self .dtype , fastpath = True
504
506
)
505
507
506
- def astype (self , dtype , copy = True ):
508
+ def astype (self , dtype : Dtype , copy : bool = True ) -> ArrayLike :
507
509
"""
508
510
Coerce this type to another dtype
509
511
@@ -519,6 +521,8 @@ def astype(self, dtype, copy=True):
519
521
520
522
"""
521
523
if is_categorical_dtype (dtype ):
524
+ dtype = cast (Union [str , CategoricalDtype ], dtype )
525
+
522
526
# GH 10696/18593
523
527
dtype = self .dtype .update_dtype (dtype )
524
528
self = self .copy () if copy else self
@@ -528,27 +532,27 @@ def astype(self, dtype, copy=True):
528
532
return np .array (self , dtype = dtype , copy = copy )
529
533
530
534
@cache_readonly
531
- def ndim (self ):
535
+ def ndim (self ) -> int :
532
536
"""
533
537
Number of dimensions of the Categorical
534
538
"""
535
539
return self ._codes .ndim
536
540
537
541
@cache_readonly
538
- def size (self ):
542
+ def size (self ) -> int :
539
543
"""
540
544
return the len of myself
541
545
"""
542
546
return len (self )
543
547
544
548
@cache_readonly
545
- def itemsize (self ):
549
+ def itemsize (self ) -> int :
546
550
"""
547
551
return the size of a single category
548
552
"""
549
553
return self .categories .itemsize
550
554
551
- def tolist (self ):
555
+ def tolist (self ) -> list :
552
556
"""
553
557
Return a list of the values.
554
558
@@ -561,7 +565,7 @@ def tolist(self):
561
565
to_list = tolist
562
566
563
567
@property
564
- def base (self ):
568
+ def base (self ) -> None :
565
569
"""
566
570
compat, we are always our own object
567
571
"""
@@ -769,7 +773,7 @@ def _set_categories(self, categories, fastpath=False):
769
773
770
774
self ._dtype = new_dtype
771
775
772
- def _set_dtype (self , dtype ) :
776
+ def _set_dtype (self , dtype : CategoricalDtype ) -> "Categorical" :
773
777
"""
774
778
Internal method for directly updating the CategoricalDtype
775
779
0 commit comments