Skip to content

Commit b1e38b1

Browse files
committed
Add nsorted to series API docs and add docstring
1 parent b6c25b3 commit b1e38b1

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

doc/source/reference/series.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ Computations / descriptive stats
152152
Series.median
153153
Series.min
154154
Series.mode
155-
Series.nlargest
156-
Series.nsmallest
157155
Series.pct_change
158156
Series.prod
159157
Series.quantile
@@ -228,6 +226,9 @@ Reshaping, sorting
228226
Series.argmin
229227
Series.argmax
230228
Series.reorder_levels
229+
Series.nlargest
230+
Series.nsmallest
231+
Series.nsorted
231232
Series.sort_values
232233
Series.sort_index
233234
Series.swaplevel

pandas/core/series.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3840,6 +3840,81 @@ def nsorted(
38403840
ascending: bool,
38413841
keep: NsmallestNlargestKeep = "first",
38423842
) -> Series:
3843+
"""
3844+
Return the top `n` elements, sorted in ascending or descending order.
3845+
3846+
Parameters
3847+
----------
3848+
n : int, default 5
3849+
Return this many descending sorted values.
3850+
ascending : bool
3851+
If True, return the smallest `n` elements. If False, return
3852+
largest `n` elements.
3853+
keep : {'first', 'last', 'all'}, default 'first'
3854+
When there are duplicate values that cannot all fit in a
3855+
Series of `n` elements:
3856+
3857+
- ``first`` : return the first `n` occurrences in order
3858+
of appearance.
3859+
- ``last`` : return the last `n` occurrences in reverse
3860+
order of appearance.
3861+
- ``all`` : keep all occurrences. This can result in a Series of
3862+
size larger than `n`.
3863+
3864+
Returns
3865+
-------
3866+
Series
3867+
The `n` top values in the Series.
3868+
3869+
See Also
3870+
--------
3871+
Series.nsmallest: Get the `n` smallest elements.
3872+
Series.nlargest: Get the `n` largest elements.
3873+
3874+
Examples
3875+
--------
3876+
>>> countries_population = {
3877+
... "Italy": 59000000,
3878+
... "France": 65000000,
3879+
... "Malta": 434000,
3880+
... "Maldives": 434000,
3881+
... "Brunei": 434000,
3882+
... "Iceland": 337000,
3883+
... "Nauru": 11300,
3884+
... "Tuvalu": 11300,
3885+
... "Anguilla": 11300,
3886+
... "Montserrat": 5200,
3887+
... }
3888+
>>> s = pd.Series(countries_population)
3889+
>>> s
3890+
Italy 59000000
3891+
France 65000000
3892+
Malta 434000
3893+
Maldives 434000
3894+
Brunei 434000
3895+
Iceland 337000
3896+
Nauru 11300
3897+
Tuvalu 11300
3898+
Anguilla 11300
3899+
Montserrat 5200
3900+
dtype: int64
3901+
3902+
Get the `n` largest elements.
3903+
3904+
>>> s.nsorted(n=3, ascending=False)
3905+
France 65000000
3906+
Italy 59000000
3907+
Malta 434000
3908+
dtype: int64
3909+
3910+
Get the `n` smallest elements.
3911+
3912+
>>> s.nsorted(n=3, ascending=True)
3913+
Montserrat 5200
3914+
Nauru 11300
3915+
Tuvalu 11300
3916+
dtype: int64
3917+
"""
38433918
return selectn.SelectNSeries(
38443919
self,
38453920
n=n,

0 commit comments

Comments
 (0)