109
109
Shape ,
110
110
SortKind ,
111
111
npt ,
112
- type_t ,
113
112
)
114
113
115
114
from pandas import (
@@ -473,8 +472,8 @@ def _internal_fill_value(self) -> int:
473
472
@classmethod
474
473
def _from_sequence (
475
474
cls , scalars , * , dtype : Dtype | None = None , copy : bool = False
476
- ) -> Categorical :
477
- return Categorical (scalars , dtype = dtype , copy = copy )
475
+ ) -> Self :
476
+ return cls (scalars , dtype = dtype , copy = copy )
478
477
479
478
@overload
480
479
def astype (self , dtype : npt .DTypeLike , copy : bool = ...) -> np .ndarray :
@@ -563,7 +562,7 @@ def to_list(self):
563
562
@classmethod
564
563
def _from_inferred_categories (
565
564
cls , inferred_categories , inferred_codes , dtype , true_values = None
566
- ):
565
+ ) -> Self :
567
566
"""
568
567
Construct a Categorical from inferred values.
569
568
@@ -632,7 +631,7 @@ def _from_inferred_categories(
632
631
@classmethod
633
632
def from_codes (
634
633
cls , codes , categories = None , ordered = None , dtype : Dtype | None = None
635
- ) -> Categorical :
634
+ ) -> Self :
636
635
"""
637
636
Make a Categorical type from codes and categories or dtype.
638
637
@@ -792,7 +791,7 @@ def _set_categories(self, categories, fastpath: bool = False) -> None:
792
791
793
792
super ().__init__ (self ._ndarray , new_dtype )
794
793
795
- def _set_dtype (self , dtype : CategoricalDtype ) -> Categorical :
794
+ def _set_dtype (self , dtype : CategoricalDtype ) -> Self :
796
795
"""
797
796
Internal method for directly updating the CategoricalDtype
798
797
@@ -808,7 +807,7 @@ def _set_dtype(self, dtype: CategoricalDtype) -> Categorical:
808
807
codes = recode_for_categories (self .codes , self .categories , dtype .categories )
809
808
return type (self )(codes , dtype = dtype , fastpath = True )
810
809
811
- def set_ordered (self , value : bool ) -> Categorical :
810
+ def set_ordered (self , value : bool ) -> Self :
812
811
"""
813
812
Set the ordered attribute to the boolean value.
814
813
@@ -822,7 +821,7 @@ def set_ordered(self, value: bool) -> Categorical:
822
821
NDArrayBacked .__init__ (cat , cat ._ndarray , new_dtype )
823
822
return cat
824
823
825
- def as_ordered (self ) -> Categorical :
824
+ def as_ordered (self ) -> Self :
826
825
"""
827
826
Set the Categorical to be ordered.
828
827
@@ -833,7 +832,7 @@ def as_ordered(self) -> Categorical:
833
832
"""
834
833
return self .set_ordered (True )
835
834
836
- def as_unordered (self ) -> Categorical :
835
+ def as_unordered (self ) -> Self :
837
836
"""
838
837
Set the Categorical to be unordered.
839
838
@@ -912,7 +911,7 @@ def set_categories(self, new_categories, ordered=None, rename: bool = False):
912
911
NDArrayBacked .__init__ (cat , codes , new_dtype )
913
912
return cat
914
913
915
- def rename_categories (self , new_categories ) -> Categorical :
914
+ def rename_categories (self , new_categories ) -> Self :
916
915
"""
917
916
Rename categories.
918
917
@@ -984,7 +983,7 @@ def rename_categories(self, new_categories) -> Categorical:
984
983
cat ._set_categories (new_categories )
985
984
return cat
986
985
987
- def reorder_categories (self , new_categories , ordered = None ):
986
+ def reorder_categories (self , new_categories , ordered = None ) -> Self :
988
987
"""
989
988
Reorder categories as specified in new_categories.
990
989
@@ -1027,7 +1026,7 @@ def reorder_categories(self, new_categories, ordered=None):
1027
1026
)
1028
1027
return self .set_categories (new_categories , ordered = ordered )
1029
1028
1030
- def add_categories (self , new_categories ) -> Categorical :
1029
+ def add_categories (self , new_categories ) -> Self :
1031
1030
"""
1032
1031
Add new categories.
1033
1032
@@ -1096,7 +1095,7 @@ def add_categories(self, new_categories) -> Categorical:
1096
1095
NDArrayBacked .__init__ (cat , codes , new_dtype )
1097
1096
return cat
1098
1097
1099
- def remove_categories (self , removals ):
1098
+ def remove_categories (self , removals ) -> Self :
1100
1099
"""
1101
1100
Remove the specified categories.
1102
1101
@@ -1152,7 +1151,7 @@ def remove_categories(self, removals):
1152
1151
1153
1152
return self .set_categories (new_categories , ordered = self .ordered , rename = False )
1154
1153
1155
- def remove_unused_categories (self ) -> Categorical :
1154
+ def remove_unused_categories (self ) -> Self :
1156
1155
"""
1157
1156
Remove categories which are not used.
1158
1157
@@ -1454,7 +1453,7 @@ def memory_usage(self, deep: bool = False) -> int:
1454
1453
"""
1455
1454
return self ._codes .nbytes + self .dtype .categories .memory_usage (deep = deep )
1456
1455
1457
- def isna (self ) -> np .ndarray :
1456
+ def isna (self ) -> npt . NDArray [ np .bool_ ] :
1458
1457
"""
1459
1458
Detect missing values
1460
1459
@@ -1475,7 +1474,7 @@ def isna(self) -> np.ndarray:
1475
1474
1476
1475
isnull = isna
1477
1476
1478
- def notna (self ) -> np .ndarray :
1477
+ def notna (self ) -> npt . NDArray [ np .bool_ ] :
1479
1478
"""
1480
1479
Inverse of isna
1481
1480
@@ -1544,8 +1543,8 @@ def value_counts(self, dropna: bool = True) -> Series:
1544
1543
# "ExtensionDtype"
1545
1544
@classmethod
1546
1545
def _empty ( # type: ignore[override]
1547
- cls : type_t [ Categorical ] , shape : Shape , dtype : CategoricalDtype
1548
- ) -> Categorical :
1546
+ cls , shape : Shape , dtype : CategoricalDtype
1547
+ ) -> Self :
1549
1548
"""
1550
1549
Analogous to np.empty(shape, dtype=dtype)
1551
1550
@@ -1651,7 +1650,7 @@ def sort_values(
1651
1650
inplace : Literal [False ] = ...,
1652
1651
ascending : bool = ...,
1653
1652
na_position : str = ...,
1654
- ) -> Categorical :
1653
+ ) -> Self :
1655
1654
...
1656
1655
1657
1656
@overload
@@ -1666,7 +1665,7 @@ def sort_values(
1666
1665
inplace : bool = False ,
1667
1666
ascending : bool = True ,
1668
1667
na_position : str = "last" ,
1669
- ) -> Categorical | None :
1668
+ ) -> Self | None :
1670
1669
"""
1671
1670
Sort the Categorical by category value returning a new
1672
1671
Categorical by default.
@@ -1769,7 +1768,7 @@ def _rank(
1769
1768
pct = pct ,
1770
1769
)
1771
1770
1772
- def _values_for_rank (self ):
1771
+ def _values_for_rank (self ) -> np . ndarray :
1773
1772
"""
1774
1773
For correctly ranking ordered categorical data. See GH#15420
1775
1774
0 commit comments