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

36 changes: 35 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,40 @@ 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.

dataframe -> DataFrame


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
----------
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be the same length as "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",

Choose a reason for hiding this comment

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

Space after ","

Copy link
Contributor

Choose a reason for hiding this comment

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

space after the commas

... "Watermelon"]).astype('category')
>>> df = pd.DataFrame({'Weight (grams)':[2000,230,160,1300]}, index=s)
Copy link
Contributor

Choose a reason for hiding this comment

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

spcae after the colon. Space after commas.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your comments ^^ !! We have already changed it.

Choose a reason for hiding this comment

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

Space after ","

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, we appreciate your comments! We have already changed it :)

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

>>> df = pd.Series(["Peter","Adam","Elisabeth","Margareth"])
Copy link
Contributor

Choose a reason for hiding this comment

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

call the ser = since it's a series :)

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

def is_interval(self):
Expand Down