@@ -272,7 +272,7 @@ class Categorical(NDArrayBackedExtensionArray, PandasObject, ObjectStringArrayMi
272
272
Attributes
273
273
----------
274
274
categories : Index
275
- The categories of this categorical
275
+ The categories of this categorical.
276
276
codes : ndarray
277
277
The codes (integer positions, which point to the categories) of this
278
278
categorical, read only.
@@ -760,23 +760,32 @@ def categories(self) -> Index:
760
760
761
761
Examples
762
762
--------
763
+ For :class:`pandas.Series`:
763
764
764
- For Series:
765
-
766
- >>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
765
+ >>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
767
766
>>> ser.cat.categories
768
767
Index(['a', 'b', 'c'], dtype='object')
769
768
770
- >>> raw_cat = pd.Categorical(["a", "b", "c", "a" ], categories=["b", "c", "d"], )
769
+ >>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a' ], categories=['b', 'c', 'd'] )
771
770
>>> ser = pd.Series(raw_cat)
772
771
>>> ser.cat.categories
773
772
Index(['b', 'c', 'd'], dtype='object')
774
773
775
- For Categorical:
774
+ For :class:`pandas. Categorical` :
776
775
777
776
>>> cat = pd.Categorical(['a', 'b'], ordered=True)
778
777
>>> cat.categories
779
778
Index(['a', 'b'], dtype='object')
779
+
780
+ For :class:`pandas.CategoricalIndex`:
781
+
782
+ >>> ci = pd.CategoricalIndex(['a', 'c', 'b', 'a', 'c', 'b'])
783
+ >>> ci.categories
784
+ Index(['a', 'b', 'c'], dtype='object')
785
+
786
+ >>> ci = pd.CategoricalIndex(['a', 'c'], categories=['c', 'b', 'a'])
787
+ >>> ci.categories
788
+ Index(['c', 'b', 'a'], dtype='object')
780
789
"""
781
790
return self .dtype .categories
782
791
@@ -787,19 +796,18 @@ def ordered(self) -> Ordered:
787
796
788
797
Examples
789
798
--------
799
+ For :class:`pandas.Series`:
790
800
791
- For Series:
792
-
793
- >>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
801
+ >>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
794
802
>>> ser.cat.ordered
795
803
False
796
804
797
- >>> raw_cat = pd.Categorical(["a", "b", "c", "a" ], ordered=True)
805
+ >>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a' ], ordered=True)
798
806
>>> ser = pd.Series(raw_cat)
799
807
>>> ser.cat.ordered
800
808
True
801
809
802
- For Categorical:
810
+ For :class:`pandas. Categorical` :
803
811
804
812
>>> cat = pd.Categorical(['a', 'b'], ordered=True)
805
813
>>> cat.ordered
@@ -808,13 +816,23 @@ def ordered(self) -> Ordered:
808
816
>>> cat = pd.Categorical(['a', 'b'], ordered=False)
809
817
>>> cat.ordered
810
818
False
819
+
820
+ For :class:`pandas.CategoricalIndex`:
821
+
822
+ >>> ci = pd.CategoricalIndex(['a', 'b'], ordered=True)
823
+ >>> ci.ordered
824
+ True
825
+
826
+ >>> ci = pd.CategoricalIndex(['a', 'b'], ordered=False)
827
+ >>> ci.ordered
828
+ False
811
829
"""
812
830
return self .dtype .ordered
813
831
814
832
@property
815
833
def codes (self ) -> np .ndarray :
816
834
"""
817
- The category codes of this categorical.
835
+ The category codes of this categorical index .
818
836
819
837
Codes are an array of integers which are the positions of the actual
820
838
values in the categories array.
@@ -825,13 +843,25 @@ def codes(self) -> np.ndarray:
825
843
Returns
826
844
-------
827
845
ndarray[int]
828
- A non-writable view of the `codes` array.
846
+ A non-writable view of the `` codes` ` array.
829
847
830
848
Examples
831
849
--------
850
+ For :class:`pandas.Categorical`:
851
+
832
852
>>> cat = pd.Categorical(['a', 'b'], ordered=True)
833
853
>>> cat.codes
834
854
array([0, 1], dtype=int8)
855
+
856
+ For :class:`pandas.CategoricalIndex`:
857
+
858
+ >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'])
859
+ >>> ci.codes
860
+ array([0, 1, 2, 0, 1, 2], dtype=int8)
861
+
862
+ >>> ci = pd.CategoricalIndex(['a', 'c'], categories=['c', 'b', 'a'])
863
+ >>> ci.codes
864
+ array([2, 0], dtype=int8)
835
865
"""
836
866
v = self ._codes .view ()
837
867
v .flags .writeable = False
@@ -915,12 +945,23 @@ def as_ordered(self) -> Self:
915
945
916
946
Examples
917
947
--------
918
- >>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
948
+ For :class:`pandas.Series`:
949
+
950
+ >>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
919
951
>>> ser.cat.ordered
920
952
False
921
953
>>> ser = ser.cat.as_ordered()
922
954
>>> ser.cat.ordered
923
955
True
956
+
957
+ For :class:`pandas.CategoricalIndex`:
958
+
959
+ >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a'])
960
+ >>> ci.ordered
961
+ False
962
+ >>> ci = ci.as_ordered()
963
+ >>> ci.ordered
964
+ True
924
965
"""
925
966
return self .set_ordered (True )
926
967
@@ -935,24 +976,36 @@ def as_unordered(self) -> Self:
935
976
936
977
Examples
937
978
--------
938
- >>> raw_cate = pd.Categorical(["a", "b", "c"],
939
- ... categories=["a", "b", "c"], ordered=True)
940
- >>> ser = pd.Series(raw_cate)
979
+ For :class:`pandas.Series`:
980
+
981
+ >>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a'], ordered=True)
982
+ >>> ser = pd.Series(raw_cat)
983
+ >>> ser.cat.ordered
984
+ True
941
985
>>> ser = ser.cat.as_unordered()
942
986
>>> ser.cat.ordered
943
987
False
988
+
989
+ For :class:`pandas.CategoricalIndex`:
990
+
991
+ >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a'], ordered=True)
992
+ >>> ci.ordered
993
+ True
994
+ >>> ci = ci.as_unordered()
995
+ >>> ci.ordered
996
+ False
944
997
"""
945
998
return self .set_ordered (False )
946
999
947
1000
def set_categories (self , new_categories , ordered = None , rename : bool = False ):
948
1001
"""
949
- Set the categories to the specified new_categories .
1002
+ Set the categories to the specified new categories .
950
1003
951
- `new_categories` can include new categories (which will result in
1004
+ `` new_categories` ` can include new categories (which will result in
952
1005
unused categories) or remove old categories (which results in values
953
- set to NaN). If `rename== True`, the categories will simple be renamed
1006
+ set to `` NaN`` ). If `` rename=True`` , the categories will simply be renamed
954
1007
(less or more items than in old categories will result in values set to
955
- NaN or in unused categories respectively).
1008
+ `` NaN`` or in unused categories respectively).
956
1009
957
1010
This method can be used to perform more than one action of adding,
958
1011
removing, and reordering simultaneously and is therefore faster than
@@ -994,23 +1047,41 @@ def set_categories(self, new_categories, ordered=None, rename: bool = False):
994
1047
995
1048
Examples
996
1049
--------
997
- >>> raw_cate = pd.Categorical(["a", "b", "c", "A"],
998
- ... categories=["a", "b", "c"], ordered=True)
999
- >>> ser = pd.Series(raw_cate)
1050
+ For :class:`pandas.Series`:
1051
+
1052
+ >>> raw_cat = pd.Categorical(['a', 'b', 'c', 'A'],
1053
+ ... categories=['a', 'b', 'c'], ordered=True)
1054
+ >>> ser = pd.Series(raw_cat)
1000
1055
>>> ser
1001
1056
0 a
1002
1057
1 b
1003
1058
2 c
1004
1059
3 NaN
1005
1060
dtype: category
1006
1061
Categories (3, object): ['a' < 'b' < 'c']
1007
- >>> ser.cat.set_categories(["A", "B", "C"], rename=True)
1062
+
1063
+ >>> ser.cat.set_categories(['A', 'B', 'C'], rename=True)
1008
1064
0 A
1009
1065
1 B
1010
1066
2 C
1011
1067
3 NaN
1012
1068
dtype: category
1013
1069
Categories (3, object): ['A' < 'B' < 'C']
1070
+
1071
+ For :class:`pandas.CategoricalIndex`:
1072
+
1073
+ >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'A'],
1074
+ ... categories=['a', 'b', 'c'], ordered=True)
1075
+ >>> ci
1076
+ CategoricalIndex(['a', 'b', 'c', nan], categories=['a', 'b', 'c'],
1077
+ ordered=True, dtype='category')
1078
+
1079
+ >>> ci.set_categories(['A', 'b', 'c'])
1080
+ CategoricalIndex([nan, 'b', 'c', nan], categories=['A', 'b', 'c'],
1081
+ ordered=True, dtype='category')
1082
+ >>> ci.set_categories(['A', 'b', 'c'], rename=True)
1083
+ CategoricalIndex(['A', 'b', 'c', nan], categories=['A', 'b', 'c'],
1084
+ ordered=True, dtype='category')
1014
1085
"""
1015
1086
1016
1087
if ordered is None :
@@ -1108,7 +1179,7 @@ def reorder_categories(self, new_categories, ordered=None) -> Self:
1108
1179
"""
1109
1180
Reorder categories as specified in new_categories.
1110
1181
1111
- `new_categories` need to include all old categories and no new category
1182
+ `` new_categories` ` need to include all old categories and no new category
1112
1183
items.
1113
1184
1114
1185
Parameters
@@ -1140,7 +1211,9 @@ def reorder_categories(self, new_categories, ordered=None) -> Self:
1140
1211
1141
1212
Examples
1142
1213
--------
1143
- >>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
1214
+ For :class:`pandas.Series`:
1215
+
1216
+ >>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
1144
1217
>>> ser = ser.cat.reorder_categories(['c', 'b', 'a'], ordered=True)
1145
1218
>>> ser
1146
1219
0 a
@@ -1149,14 +1222,24 @@ def reorder_categories(self, new_categories, ordered=None) -> Self:
1149
1222
3 a
1150
1223
dtype: category
1151
1224
Categories (3, object): ['c' < 'b' < 'a']
1152
- >>> ser = ser.sort_values()
1153
- >>> ser
1225
+
1226
+ >>> ser.sort_values()
1154
1227
2 c
1155
1228
1 b
1156
1229
0 a
1157
1230
3 a
1158
1231
dtype: category
1159
1232
Categories (3, object): ['c' < 'b' < 'a']
1233
+
1234
+ For :class:`pandas.CategoricalIndex`:
1235
+
1236
+ >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a'])
1237
+ >>> ci
1238
+ CategoricalIndex(['a', 'b', 'c', 'a'], categories=['a', 'b', 'c'],
1239
+ ordered=False, dtype='category')
1240
+ >>> ci.reorder_categories(['c', 'b', 'a'], ordered=True)
1241
+ CategoricalIndex(['a', 'b', 'c', 'a'], categories=['c', 'b', 'a'],
1242
+ ordered=True, dtype='category')
1160
1243
"""
1161
1244
if (
1162
1245
len (self .categories ) != len (new_categories )
0 commit comments