from pandas.api.types import CategoricalDtype
df = pd.DataFrame({"A": np.arange(6), "B": list("aabbca")})
df["B"] = df["B"].astype(CategoricalDtype(list("cab")))
df["B"].cat.categories
df2 = df.set_index("B")
df2.loc["a"]
df2.loc["a"].index
df2.sort_index()
df2.groupby(level=0).sum()
df2.groupby(level=0).sum().index
df3 = pd.DataFrame(
{"A": np.arange(3), "B": pd.Series(list("abc")).astype("category")}
)
df3 = df3.set_index("B")
df3.reindex(["a", "e"])
df3.reindex(["a", "e"]).index
df3.reindex(pd.Categorical(["a", "e"], categories=list("abe")))
df3.reindex(pd.Categorical(["a", "e"], categories=list("abe"))).index