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

37 changes: 36 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,41 @@ def is_object(self):
return is_object_dtype(self.dtype)

def is_categorical(self):
"""
Check if the Index type is categorical.

The given object must be a pandas DataFrame or pandas Series.
Copy link
Contributor

Choose a reason for hiding this comment

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

this only applies to a Series or an Index, IOW it must be 1D

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 @jreback ! I have changed the exended summary. Is it better now?


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 index is categorical.

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
--------
>>> s = pd.Series(["Watermelon", "Orange", "Apple",
... "Watermelon"]).astype('category')
>>> df = pd.DataFrame({'Weight (grams)': [2000, 230, 160, 1300]},
Copy link
Contributor

Choose a reason for hiding this comment

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

don't create a DataFrame here just to use its index, just use the series above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, Thank you! I have changed the examples.

... index=s)
>>> df
Weight (grams)
Watermelon 2000
Orange 230
Apple 160
Watermelon 1300
>>> df.index.is_categorical()
True

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

def is_interval(self):
Expand Down