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
28 changes: 23 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,19 +1110,37 @@ def to_series(self, index=None, name=None):
return Series(self._to_embed(), index=index, name=name)

def to_frame(self, index=True):
"""
Create a DataFrame with a column containing the Index.
"""Create a DataFrame with a column containing the Index.

.. versionadded:: 0.21.0

Parameters
----------
index : boolean, default True
Set the index of the returned DataFrame as the original Index.
index : boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

@datapythonista @jorisvandenbossche defaults on the same line, yes?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the documentation still says to use it the numpy way (in brackets after the description), but in pandas-dev the consensus seems to be keeping the pandas way, which was already right in this case. Will update the documentation asap and send an email with the last minute changes to the organizers

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the docstring guide has been updated, but apparently only in the source: https://github.com/python-sprints/python-sprints.github.io/blob/master/pandas/guide/source/pandas_docstring.rst and not yet in the online version

Set the index of the returned DataFrame as the original Index
(default 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