Skip to content

DOC: update the pandas.Series.sort_index docstring #20248

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

Conversation

dsakuma
Copy link
Contributor

@dsakuma dsakuma commented Mar 10, 2018

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:

################################################################################
##################### Docstring (pandas.Series.sort_index) #####################
################################################################################

Sort Series by index labels.

Returns a new Series sorted by label if `inplace` argument is `False`,
otherwise updates the original series and returns `null`.

Parameters
----------
axis : int, default 0
    Axis to direct sorting. This can only be 0 for Series.
level : int, optional
    If not None, sort on values in specified index level(s).
ascending : bool, default true
    Sort ascending vs. descending.
inplace : bool, default False
    If True, perform operation in-place.
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
    Choice of sorting algorithm. See also :func:`numpy.sort` for more
    information.  'mergesort' is the only stable algorithm. For
    DataFrames, this option is only applied when sorting on a single
    column or label.
na_position : {'first', 'last'}, default 'last'
    If 'first' puts NaNs at the beginning, 'last' puts NaNs at the end.
    Not implemented for MultiIndex.
sort_remaining : bool, default True
    If true and sorting by level and index is multilevel, sort by other
    levels too (in order) after sorting by specified level.

Returns
-------
pandas.Series
    The original Series sorted by the labels

See Also
--------
DataFrame.sort_index: Sort DataFrame by the index
DataFrame.sort_values: Sort DataFrame by the value
Series.sort_values : Sort Series by the value

Examples
--------
>>> s = pd.Series(['a', 'b', 'c', 'd'], index=[3, 2, 1, 4])
>>> s.sort_index()
1    c
2    b
3    a
4    d
dtype: object

Sort Descending

>>> s.sort_index(ascending=False)
4    d
3    a
2    b
1    c
dtype: object

Sort Inplace

>>> s.sort_index(inplace=True)
>>> s
1    c
2    b
3    a
4    d
dtype: object

By default NaNs are put at the end, but use `na_position` to place
them at the beginning

>>> s = pd.Series(['a', 'b', 'c', 'd'], index=[3, 2, 1, np.nan])
>>> s.sort_index(na_position='first')
NaN     d
 1.0    c
 2.0    b
 3.0    a
dtype: object

Specify index level to sort

>>> arrays = [np.array(['qux', 'qux', 'foo', 'foo',
...                     'baz', 'baz', 'bar', 'bar']),
...           np.array(['two', 'one', 'two', 'one',
...                     'two', 'one', 'two', 'one'])]
>>> s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=arrays)
>>> s.sort_index(level=1)
bar  one    8
baz  one    6
foo  one    4
qux  one    2
bar  two    7
baz  two    5
foo  two    3
qux  two    1
dtype: int64

Does not sort by remaining levels when sorting by levels

>>> s.sort_index(level=1, sort_remaining=False)
qux  one    2
foo  one    4
baz  one    6
bar  one    8
qux  two    1
foo  two    3
baz  two    5
bar  two    7
dtype: int64

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

Docstring for "pandas.Series.sort_index" correct. :)

@pep8speaks
Copy link

pep8speaks commented Mar 10, 2018

Hello @dsakuma! Thanks for updating the PR.

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

Comment last updated on March 12, 2018 at 16:59 Hours UTC

Copy link
Contributor

@villasv villasv left a comment

Choose a reason for hiding this comment

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

Looking good


Returns
-------
sorted_obj : Series
Copy link
Contributor

Choose a reason for hiding this comment

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

Return values don't need names unless with tuples. Also missing description.


>>> import numpy as np
>>> arrays = [np.array(['qux', 'qux', 'foo', 'foo',\
'baz', 'baz', 'bar', 'bar']),
Copy link
Contributor

Choose a reason for hiding this comment

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

Bad indentation.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm. very minor change. ping when pushed.


Specify index level to sort

>>> import numpy as np
Copy link
Contributor

Choose a reason for hiding this comment

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

don't need the numpy import

@jreback jreback added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Mar 11, 2018
@jreback jreback added this to the 0.23.0 milestone Mar 11, 2018

See Also
--------
sort_values : Sort by the value
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 DataFrame.sort_index, DataFrame.sort_values


See Also
--------
sort_values : Sort by the value
Copy link
Contributor

Choose a reason for hiding this comment

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

make this Series.sort_values

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

def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
kind='quicksort', na_position='last', sort_remaining=True):
"""
Sort object by labels.
Copy link
Member

Choose a reason for hiding this comment

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

"Sort object by labels." -> "Sort Series by index labels." (if we make the docstring specific for Series and not share it with DataFrame, we can make it more specific)

Parameters
----------
axis : int, default 0
Axis to direct sorting.
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 mention this can only be 0 for Series ?

----------
axis : int, default 0
Axis to direct sorting.
level : int, 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.

" default None" -> "optional"

Axis to direct sorting.
level : int, default None
If not None, sort on values in specified index level(s).
ascending : boolean, default true
Copy link
Member

Choose a reason for hiding this comment

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

boolean -> bool (consistent with keyword below)

inplace : bool, default False
If True, perform operation in-place.
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
Choice of sorting algorithm. See also ndarray.np.sort for more
Copy link
Member

Choose a reason for hiding this comment

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

"ndarray.np.sort" -> "numpy.sort"
(or even better to link to the docs: :func:`numpy.sort`)

If True, perform operation in-place.
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
Choice of sorting algorithm. See also ndarray.np.sort for more
information. `mergesort` is the only stable algorithm. For
Copy link
Member

Choose a reason for hiding this comment

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

you can use normal quotes in this case (like 'mergesort') to indicate it is a string

DataFrames, this option is only applied when sorting on a single
column or label.
na_position : {'first', 'last'}, default 'last'
If `first` puts NaNs at the beginning, `last` puts NaNs at the end.
Copy link
Member

Choose a reason for hiding this comment

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

same here (using normal quotes instead of backticks around first and last)


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

Choose a reason for hiding this comment

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

spaces after comma (PEP8)

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.

Added some more comments on the examples. We are getting there!


Examples
--------
>>> s = pd.Series(['a','b','c','d'], index=[3,2,1,4])
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 add spaces after the comma for PEP8 in all examples?
Maybe my previous comment was unclear, but the ['a', 'b', 'c', 'd'], was already good, but commented for adding spaces in [3,2,1,4]

DataFrames, this option is only applied when sorting on a single
column or label.
na_position : {'first', 'last'}, default 'last'
If `first` puts NaNs at the beginning, 'last' puts NaNs at the end.
Copy link
Member

Choose a reason for hiding this comment

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

'first' also with normal single quotes


Sort Descending

>>> s = pd.Series(['a','b','c','d'], index=[3,2,1,4])
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, the examples are run in the same process, so it still knows the s from the previous one. So this full lines can be removed.


Sort Inplace

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

Choose a reason for hiding this comment

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

same here


Sort placing NaNs at first

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

Choose a reason for hiding this comment

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

this one is fine to keep since it is a different series (but same comment regarding PEP8)

4 d
dtype: object

Sort placing NaNs at first
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 change "Sort placing NaNs at first" in "By default NaNs are put at the end, but use na_position to place them at the beginning"

... 'baz','baz','bar','bar']),
... np.array(['two','one','two','one',
... 'two','one','two','one'])]
>>> s = pd.Series([1,2,3,4,5,6,7,8], index=arrays)
Copy link
Member

Choose a reason for hiding this comment

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

pep8

... 'baz','baz','bar','bar']),
... np.array(['two','one','two','one',
... 'two','one','two','one'])]
>>> s = pd.Series([1,2,3,4,5,6,7,8], index=arrays)
Copy link
Member

Choose a reason for hiding this comment

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

Sam here, this does not need to be redefined

@dsakuma
Copy link
Contributor Author

dsakuma commented Mar 12, 2018

@jorisvandenbossche, @jreback and @villasv Thanks for the comments!

@jorisvandenbossche jorisvandenbossche merged commit fc32727 into pandas-dev:master Mar 12, 2018
@jorisvandenbossche
Copy link
Member

@dsakuma Thanks a lot!

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.

5 participants