Skip to content
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

DOC: Extend docstring pandas core index to_frame method #20036

Merged
20 changes: 19 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,25 @@ def to_frame(self, index=True):

Returns
-------
DataFrame : a DataFrame containing the original Index data.
DataFrame
DataFrame containing the original Index data.

Examples
--------
>>> idx = pd.Index(['Ant', 'Bear', 'Cow'])
Copy link
Member

Choose a reason for hiding this comment

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

Minor thing, but it'd probably look a bit nicer if you create the index with name='animal'. :) For the rest looks great to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I do think it is interesting when creating a new index:

>>> idx.to_frame(index = False)
  animal
0    Ant
1   Bear
2    Cow

but when using the same as index and column, it does not get 'nicer':

>>> idx.to_frame(index = True)
       animal
animal       
Ant       Ant
Bear     Bear
Cow       Cow

>>> idx.to_frame()
0
Ant Ant
Bear Bear
Cow Cow

By default, the original Index is reused. To enforce a new Index:

>>> idx.to_frame(index=False)
0
0 Ant
1 Bear
2 Cow
"""

from pandas import DataFrame
Expand Down