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 @@ -1156,7 +1156,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
Cow Cow
Expand Down Expand Up @@ -1471,6 +1471,39 @@ def is_object(self):
return is_object_dtype(self.dtype)

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree! Thank you :)

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the See Also!

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