Skip to content

DOC: update the pandas.Index.is_categorical docstring #20167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

35 changes: 34 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ def to_frame(self, index=True):
--------
>>> idx = pd.Index(['Ant', 'Bear', 'Cow'], name='animal')
>>> idx.to_frame()
animal
animal
animal
Ant Ant
Bear Bear
Expand Down Expand Up @@ -1511,6 +1511,39 @@ def is_object(self):
return is_object_dtype(self.dtype)

def is_categorical(self):
"""
Check if the Index holds categorical data.

Returns
-------
boolean
True if the Index is categorical.

See Also
--------
CategoricalIndex : Index for categorical data.

Examples
--------
>>> idx = pd.Index(["Watermelon", "Orange", "Apple",
... "Watermelon"]).astype("category")
>>> idx.is_categorical()
True

>>> idx = pd.Index([1, 3, 5, 7])
>>> idx.is_categorical()
False

>>> s = pd.Series(["Peter", "Víctor", "Elisabeth", "Mar"])
>>> s
0 Peter
1 Víctor
2 Elisabeth
3 Mar
dtype: object
>>> s.index.is_categorical()
False
"""
return self.inferred_type in ['categorical']

def is_interval(self):
Expand Down