Skip to content

DOC: update the Series.reset_index DocString #20107

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

Merged
merged 12 commits into from
Mar 15, 2018
90 changes: 81 additions & 9 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,36 +1002,88 @@ def _set_value(self, label, value, takeable=False):

def reset_index(self, level=None, drop=False, name=None, inplace=False):
"""
Analogous to the :meth:`pandas.DataFrame.reset_index` function, see
docstring there.
Generate a new DataFrame with columns containing the data and
Copy link
Contributor

Choose a reason for hiding this comment

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

this needs to be a single line.

the index of the original Series, or, if drop is True,
a Series with numeric index.
Copy link
Member

Choose a reason for hiding this comment

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

Short summary must fit in one line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's hard to summarize it! I'll try!


For an Index Series, the results is a two-column DataFrame with
Copy link
Contributor

Choose a reason for hiding this comment

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

Index Series doesn't make sense, more like: For a Series with a single level index (Index)

index tourned in column.
For a MultiIndex Series, the results is a multi-column
Copy link
Contributor

Choose a reason for hiding this comment

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

for a Series with a multi-level index (MultiIndex)

DataFrame with each level being turned into a column.
The new columns will be named level_n (with n increasing frm 0)
if the name is None.

Parameters
----------
level : int, str, tuple, or list, default None
Only remove the given levels from the index. Removes all levels by
default
drop : boolean, default False
Do not try to insert index into dataframe columns
default.
drop : bool, default False
Do not try to insert index into dataframe columns.
name : object, default None
The name of the column corresponding to the Series values
inplace : boolean, default False
Modify the Series in place (do not create a new object)
The name of the column corresponding to the Series values.
inplace : `bool`, default False
Modify the Series in place (do not create a new object).

Returns
----------
resetted : DataFrame, or Series if drop == True
reset : DataFrame, or Series if drop == True
Copy link
Member

Choose a reason for hiding this comment

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

Check the first example here to see how the output of Returns is: https://python-sprints.github.io/pandas/guide/pandas_docstring.html#section-4-returns-or-yields

Also, not your change, but the hyphens under return are too long.


See Also
--------
pandas.DataFrame.reset_index: Analogous function for DataFrame
Copy link
Contributor

Choose a reason for hiding this comment

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

link to pandas.Series.set_index as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Series does not have a method set_index(), did you mean DataFrame.set_index()?

s.set_index()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ludus/develop/pandas-meetup/pandas/pandas/core/generic.py", line 4046, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'set_index'


Examples
--------

Generate a pandas.Series.

>>> s = pd.Series([1, 2, 3, 4], index=pd.Index(['a', 'b', 'c', 'd'],
... name = 'idx'))
Copy link
Member

Choose a reason for hiding this comment

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

spaces around = don't follow PEP-8.


To generate a pandas.DataFrame with default index,
call the reset_index() method.
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need to say 'call the reset_index() method'


>>> s.reset_index()
idx 0
0 a 1
1 b 2
2 c 3
3 d 4

To specify the name of the column corrisponding
Copy link
Member

Choose a reason for hiding this comment

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

corrIsponding.

Copy link
Contributor

Choose a reason for hiding this comment

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

sp

to the Series values, use the name parameter.

>>> s.reset_index(name='values')
idx values
0 a 1
1 b 2
2 c 3
3 d 4

To generate a new pandas.Series with the default
Copy link
Contributor

Choose a reason for hiding this comment

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

can just say Series

index, set the drop parameter to True.

>>> s.reset_index(drop=True)
0 1
1 2
2 3
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add an example with name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, for name and for inplace

3 4
dtype: int64

To update the Series in place, without generating a new one
set inplace to True. Note that it also requires drop=True.

>>> s.reset_index(inplace=True, drop=True)
>>> s
0 1
1 2
2 3
3 4
dtype: int64

Generate a MultiIndex Series.

>>> arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo',
... 'foo', 'qux', 'qux']),
... np.array(['one', 'two', 'one', 'two', 'one', 'two',
Copy link
Member

Choose a reason for hiding this comment

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

If I'm not wrong, just 4 rows should be enough to illustrate the example. That would make it more compact, and easier to understand for the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, that was an existing example!

Expand All @@ -1040,6 +1092,10 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
... range(8),
... index=pd.MultiIndex.from_arrays(arrays,
... names=['a', 'b']))

To remove a specific level from the Index, use the
level parameter.

>>> s2.reset_index(level='a')
a 0
b
Expand All @@ -1051,7 +1107,23 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
two foo 5
one qux 6
two qux 7

If level parameter is not set, all levels are removed
from the Index.

>>> s2.reset_index()
a b 0
0 bar one 0
1 bar two 1
2 baz one 2
3 baz two 3
4 foo one 4
5 foo two 5
6 qux one 6
7 qux two 7
"""


inplace = validate_bool_kwarg(inplace, 'inplace')
if drop:
new_index = com._default_index(len(self))
Expand Down