@@ -875,6 +875,15 @@ def as_ordered(self) -> Self:
875
875
-------
876
876
Categorical
877
877
Ordered Categorical.
878
+
879
+ Examples
880
+ --------
881
+ >>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
882
+ >>> ser.cat.ordered
883
+ False
884
+ >>> ser = ser.cat.as_ordered()
885
+ >>> ser.cat.ordered
886
+ True
878
887
"""
879
888
return self .set_ordered (True )
880
889
@@ -886,6 +895,15 @@ def as_unordered(self) -> Self:
886
895
-------
887
896
Categorical
888
897
Unordered Categorical.
898
+
899
+ Examples
900
+ --------
901
+ >>> raw_cate = pd.Categorical(["a", "b", "c"],
902
+ ... categories=["a", "b", "c"], ordered=True)
903
+ >>> ser = pd.Series(raw_cate)
904
+ >>> ser = ser.cat.as_unordered()
905
+ >>> ser.cat.ordered
906
+ False
889
907
"""
890
908
return self .set_ordered (False )
891
909
@@ -936,6 +954,26 @@ def set_categories(self, new_categories, ordered=None, rename: bool = False):
936
954
add_categories : Add new categories.
937
955
remove_categories : Remove the specified categories.
938
956
remove_unused_categories : Remove categories which are not used.
957
+
958
+ Examples
959
+ --------
960
+ >>> raw_cate = pd.Categorical(["a", "b", "c", "A"],
961
+ ... categories=["a", "b", "c"], ordered=True)
962
+ >>> ser = pd.Series(raw_cate)
963
+ >>> ser
964
+ 0 a
965
+ 1 b
966
+ 2 c
967
+ 3 NaN
968
+ dtype: category
969
+ Categories (3, object): ['a' < 'b' < 'c']
970
+ >>> ser.cat.set_categories(["A", "B", "C"], rename=True)
971
+ 0 A
972
+ 1 B
973
+ 2 C
974
+ 3 NaN
975
+ dtype: category
976
+ Categories (3, object): ['A' < 'B' < 'C']
939
977
"""
940
978
941
979
if ordered is None :
@@ -1062,6 +1100,26 @@ def reorder_categories(self, new_categories, ordered=None) -> Self:
1062
1100
remove_categories : Remove the specified categories.
1063
1101
remove_unused_categories : Remove categories which are not used.
1064
1102
set_categories : Set the categories to the specified ones.
1103
+
1104
+ Examples
1105
+ --------
1106
+ >>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
1107
+ >>> ser = ser.cat.reorder_categories(['c', 'b', 'a'], ordered=True)
1108
+ >>> ser
1109
+ 0 a
1110
+ 1 b
1111
+ 2 c
1112
+ 3 a
1113
+ dtype: category
1114
+ Categories (3, object): ['c' < 'b' < 'a']
1115
+ >>> ser = ser.sort_values()
1116
+ >>> ser
1117
+ 2 c
1118
+ 1 b
1119
+ 0 a
1120
+ 3 a
1121
+ dtype: category
1122
+ Categories (3, object): ['c' < 'b' < 'a']
1065
1123
"""
1066
1124
if (
1067
1125
len (self .categories ) != len (new_categories )
@@ -2663,6 +2721,17 @@ def _delegate_property_set(self, name: str, new_values): # type: ignore[overrid
2663
2721
def codes (self ) -> Series :
2664
2722
"""
2665
2723
Return Series of codes as well as the index.
2724
+
2725
+ Examples
2726
+ --------
2727
+ >>> raw_cate = pd.Categorical(["a", "b", "c", "a"], categories=["a", "b"])
2728
+ >>> ser = pd.Series(raw_cate)
2729
+ >>> ser.cat.codes
2730
+ 0 0
2731
+ 1 1
2732
+ 2 -1
2733
+ 3 0
2734
+ dtype: int8
2666
2735
"""
2667
2736
from pandas import Series
2668
2737
0 commit comments