@@ -875,6 +875,15 @@ def as_ordered(self) -> Self:
875875 -------
876876 Categorical
877877 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
878887 """
879888 return self .set_ordered (True )
880889
@@ -886,6 +895,15 @@ def as_unordered(self) -> Self:
886895 -------
887896 Categorical
888897 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
889907 """
890908 return self .set_ordered (False )
891909
@@ -936,6 +954,26 @@ def set_categories(self, new_categories, ordered=None, rename: bool = False):
936954 add_categories : Add new categories.
937955 remove_categories : Remove the specified categories.
938956 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']
939977 """
940978
941979 if ordered is None :
@@ -1062,6 +1100,26 @@ def reorder_categories(self, new_categories, ordered=None) -> Self:
10621100 remove_categories : Remove the specified categories.
10631101 remove_unused_categories : Remove categories which are not used.
10641102 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']
10651123 """
10661124 if (
10671125 len (self .categories ) != len (new_categories )
@@ -2663,6 +2721,17 @@ def _delegate_property_set(self, name: str, new_values): # type: ignore[overrid
26632721 def codes (self ) -> Series :
26642722 """
26652723 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
26662735 """
26672736 from pandas import Series
26682737
0 commit comments