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

Conversation

ludusrusso
Copy link
Contributor

Checklist for the pandas documentation sprint (ignore this if you are doing
an unrelated PR):

  • PR title is "DOC: update the docstring"
  • The validation script passes: scripts/validate_docstrings.py <your-function-or-method>
  • The PEP8 style check passes: git diff upstream/master -u -- "*.py" | flake8 --diff
  • The html version looks good: python doc/make.py --single <your-function-or-method>
  • It has been proofread on language by another sprint participant

Please include the output of the validation script below between the "```" ticks:

# paste output of "scripts/validate_docstrings.py <your-function-or-method>" here
# between the "```" (remove this comment, but keep the "```")

################################################################################
############## Docstring (pandas.core.series.Series.reset_index)  ##############
################################################################################

Reset the index of the Serie.

For a Serie with multi-level index, return a new Serie with labeling
information in the columns under the index names, defaulting to ‘level_0’,
‘level_1’, etc. if any are None. For a standard index,
the index name will be used (if set), otherwise a default
‘index’ or ‘level_0’ (if ‘index’ is already taken) will be used.

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.
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).

Returns
----------
resetted : `DataFrame`, or Series if `drop == True`

See Also
--------
:meth:`pandas.DataFrame.reset_index`: Analogous funciton for DataFrame

Examples
--------
>>> s = pd.Series([1, 2, 3, 4], index=pd.Index(['a', 'b', 'c', 'd'],
...                                            name = 'idx'))
>>> s.reset_index()
  idx  0
0   a  1
1   b  2
2   c  3
3   d  4

>>> arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo',
...                     'foo', 'qux', 'qux']),
...           np.array(['one', 'two', 'one', 'two', 'one', 'two',
...                     'one', 'two'])]
>>> s2 = pd.Series(
...     range(8),
...     index=pd.MultiIndex.from_arrays(arrays,
...                                     names=['a', 'b']))
>>> s2.reset_index(level='a')
       a  0
b
one  bar  0
two  bar  1
one  baz  2
two  baz  3
one  foo  4
two  foo  5
one  qux  6
two  qux  7

################################################################################
################################## Validation ##################################
################################################################################

Docstring for "pandas.core.series.Series.reset_index" correct. :)

@pep8speaks
Copy link

pep8speaks commented Mar 10, 2018

Hello @ludusrusso! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on March 15, 2018 at 20:46 Hours UTC

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Added some comments

Can you add an examples with drop=True as well?

@@ -1002,24 +1002,33 @@ 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.
Reset the index of the Serie.
Copy link
Member

Choose a reason for hiding this comment

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

General comment: Serie -> Series (the singular of Series is still Series :))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Solved tnx!

Copy link
Member

Choose a reason for hiding this comment

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

Did you already push? Because this one is not yet fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I missed it!


Parameters
----------
level : int, str, tuple, or list, default None
level : `int`, `str`, `tuple`, or `list`, default `None`
Copy link
Member

Choose a reason for hiding this comment

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

Those quotes in the type description is not needed

docstring there.
Reset the index of the Serie.

For a Serie with multi-level index, return a new Serie with labeling
Copy link
Member

Choose a reason for hiding this comment

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

I would start the explanation with the standard case of normal index


See Also
--------
:meth:`pandas.DataFrame.reset_index`: Analogous funciton for DataFrame
Copy link
Member

Choose a reason for hiding this comment

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

funciton -> function

:meth:`pandas.DataFrame.reset_index` -> DataFrame.reset_index (without any mark-up)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

DataFrame.reset_index I've removed the mark-up, but are you sure about that? It should be nice tu have a link the the related method.

@jreback jreback added Docs Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Mar 10, 2018
docstring there.
Reset the index of the Serie.

For a standard index, the index name will be used (if set),
Copy link
Contributor

Choose a reason for hiding this comment

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

just say Index

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

For a standard index, the index name will be used (if set),
otherwise a default index or `level_0` (if `index` is already taken)
will be used.
For a Series with multi-level index, return a new Series with labeling
Copy link
Contributor

Choose a reason for hiding this comment

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

say MultiIndex

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

inplace : boolean, default False
Modify the Series in place (do not create a new object)
default.
drop : `boolean`, default `False`
Copy link
Contributor

Choose a reason for hiding this comment

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

boolean -> bool

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tnx

>>> 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


Returns
----------
resetted : DataFrame, or Series if drop == True
resetted : `DataFrame`, or Series if `drop == True`
Copy link
Contributor

Choose a reason for hiding this comment

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

Resetted should probably just "reset"

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!

@ludusrusso
Copy link
Contributor Author

Update pull request after review! Tnx!

Reset the index of the Serie.

For an Index, the index name will be used (if set),
otherwise a default index or `level_0` (if `index` is already taken)
Copy link
Contributor

Choose a reason for hiding this comment

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

the default IS 'index', can you make this a bit more clear


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

Choose a reason for hiding this comment

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

I don't recall whether we are single ticking Series, DataFrame in doc-strings. @datapythonista @jorisvandenbossche

Copy link
Member

Choose a reason for hiding this comment

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

decision was to not use ticks in this case

Copy link
Contributor

Choose a reason for hiding this comment

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

ok @ludusrusso can you update


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

Choose a reason for hiding this comment

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

no spaces around the equals

Do not try to insert index into dataframe columns.
name : `object`, default `None`
The name of the column corresponding to the Series values.
inplace : `bool`, default `False`
Copy link
Member

Choose a reason for hiding this comment

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

Can you also remove the backticks here? In general: no backticks in the type descriptions

2 c 3
3 d 4

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

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 re-define the series, you can reuse from the previous code block

Copy link
Member

Choose a reason for hiding this comment

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

Can you also put some explanation sentence between the examples?

@ludusrusso
Copy link
Contributor Author

Thanks you all! I've updated the examples with comments!

@@ -1002,36 +1002,86 @@ 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.
Reset the index of the Series.
Copy link
Contributor

Choose a reason for hiding this comment

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

so this is not correct, .reset_index(drop=False) (the default) on a Series will return a DataFrame. The columns will be the name of the Series and a new column of the index, with its name being the name of the column or 'index' if its not named.

For an Index, the index name will be used (if set),
otherwise a default index or level_0 (if index is already taken)
will be used.
For a MultiIndex, return a new Series with labeling
Copy link
Contributor

Choose a reason for hiding this comment

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

you will get a multi-column DataFrame, with each level being turned into a column, here the levels will be named level_n if the name is None.


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

Choose a reason for hiding this comment

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

no back ticks


Examples
--------

Generate a pandas.Series with a given Index.
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment is a bit odd here

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

To generate a pandas.DataFrame with resetted index,
Copy link
Contributor

Choose a reason for hiding this comment

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

resetted -> default index

2 c 3
3 d 4

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

Choose a reason for hiding this comment

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

resetted -> default index

@jorisvandenbossche
Copy link
Member

@ludusrusso did you run the validation script again after updating?

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

Looks good in general, but still needs some work.

docstring there.
Generate a new DataFrame with columns containing the data and
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!


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.

>>> 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.


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.

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!

@@ -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.

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)


For an Index Series, the results is a two-column DataFrame with
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)


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'

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

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
Contributor

Choose a reason for hiding this comment

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

sp

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

@codecov
Copy link

codecov bot commented Mar 11, 2018

Codecov Report

Merging #20107 into master will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #20107      +/-   ##
==========================================
+ Coverage   91.72%   91.72%   +<.01%     
==========================================
  Files         150      150              
  Lines       49149    49159      +10     
==========================================
+ Hits        45083    45093      +10     
  Misses       4066     4066
Flag Coverage Δ
#multiple 90.11% <ø> (ø) ⬆️
#single 41.85% <ø> (ø) ⬆️
Impacted Files Coverage Δ
pandas/core/series.py 93.85% <ø> (-0.01%) ⬇️
pandas/core/base.py 96.78% <0%> (-0.02%) ⬇️
pandas/core/indexes/datetimes.py 95.64% <0%> (-0.01%) ⬇️
pandas/core/indexes/base.py 96.66% <0%> (-0.01%) ⬇️
pandas/core/strings.py 98.32% <0%> (ø) ⬆️
pandas/core/indexing.py 93.02% <0%> (ø) ⬆️
pandas/core/indexes/timedeltas.py 91.03% <0%> (ø) ⬆️
pandas/core/indexes/multi.py 95.06% <0%> (ø) ⬆️
pandas/core/generic.py 95.84% <0%> (ø) ⬆️
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4131149...8b0cefd. Read the comment docs.

@ludusrusso
Copy link
Contributor Author

@jorisvandenbossche you're right.. I've checked it this time!

@jorisvandenbossche jorisvandenbossche merged commit 6a2ece9 into pandas-dev:master Mar 15, 2018
@jorisvandenbossche
Copy link
Member

Very nice docstring, thanks @ludusrusso !

@jorisvandenbossche jorisvandenbossche added this to the 0.23.0 milestone Mar 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants