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
@@ -473,7 +475,7 @@ def categories(self, categories):
473
475
self._dtype = new_dtype
474
476
475
477
@property
476
- def ordered(self):
478
+ def ordered(self) -> OrderedType :
477
479
"""
478
480
Whether the categories have an ordered relationship.
479
481
"""
@@ -487,11 +489,11 @@ def dtype(self) -> CategoricalDtype:
487
489
return self._dtype
488
490
489
491
@property
490
- def _ndarray_values(self):
492
+ def _ndarray_values(self) -> np.ndarray :
491
493
return self.codes
492
494
493
495
@property
494
- def _constructor(self):
496
+ def _constructor(self) -> Type["Categorical"] :
495
497
return Categorical
496
498
497
499
@classmethod
@@ -502,15 +504,15 @@ def _formatter(self, boxed=False):
502
504
# Defer to CategoricalFormatter's formatter.
503
505
return None
504
506
505
- def copy(self):
507
+ def copy(self) -> "Categorical" :
506
508
"""
507
509
Copy constructor.
508
510
"""
509
511
return self._constructor(
510
512
values=self._codes.copy(), dtype=self.dtype, fastpath=True
511
513
)
512
514
513
- def astype(self, dtype, copy= True):
515
+ def astype(self, dtype: Dtype , copy: bool = True) -> ArrayLike :
514
516
"""
515
517
Coerce this type to another dtype
516
518
@@ -523,6 +525,8 @@ def astype(self, dtype, copy=True):
523
525
object is returned.
524
526
"""
525
527
if is_categorical_dtype(dtype):
528
+ dtype = cast(Union[str, CategoricalDtype], dtype)
529
+
526
530
# GH 10696/18593
527
531
dtype = self.dtype.update_dtype(dtype)
528
532
self = self.copy() if copy else self
@@ -532,27 +536,27 @@ def astype(self, dtype, copy=True):
532
536
return np.array(self, dtype=dtype, copy=copy)
533
537
534
538
@cache_readonly
535
- def ndim(self):
539
+ def ndim(self) -> int :
536
540
"""
537
541
Number of dimensions of the Categorical
538
542
"""
539
543
return self._codes.ndim
540
544
541
545
@cache_readonly
542
- def size(self):
546
+ def size(self) -> int :
543
547
"""
544
548
return the len of myself
545
549
"""
546
550
return len(self)
547
551
548
552
@cache_readonly
549
- def itemsize(self):
553
+ def itemsize(self) -> int :
550
554
"""
551
555
return the size of a single category
552
556
"""
553
557
return self.categories.itemsize
554
558
555
- def tolist(self):
559
+ def tolist(self) -> list :
556
560
"""
557
561
Return a list of the values.
558
562
@@ -565,7 +569,7 @@ def tolist(self):
565
569
to_list = tolist
566
570
567
571
@property
568
- def base(self):
572
+ def base(self) -> None :
569
573
"""
570
574
compat, we are always our own object
571
575
"""
@@ -773,7 +777,7 @@ def _set_categories(self, categories, fastpath=False):
773
777
774
778
self._dtype = new_dtype
775
779
776
- def _set_dtype(self, dtype) :
780
+ def _set_dtype(self, dtype: CategoricalDtype) -> "Categorical" :
777
781
"""
778
782
Internal method for directly updating the CategoricalDtype
779
783
0 commit comments