@@ -1961,10 +1961,110 @@ def nlargest(
19611961 result = self ._python_apply_general (f , data , not_indexed_same = True )
19621962 return result
19631963
1964- @doc (Series .nsmallest .__doc__ )
19651964 def nsmallest (
19661965 self , n : int = 5 , keep : Literal ["first" , "last" , "all" ] = "first"
19671966 ) -> Series :
1967+ """
1968+ Return the smallest `n` elements.
1969+
1970+ Parameters
1971+ ----------
1972+ n : int, default 5
1973+ Return this many ascending sorted values.
1974+ keep : {'first', 'last', 'all'}, default 'first'
1975+ When there are duplicate values that cannot all fit in a
1976+ Series of `n` elements:
1977+
1978+ - ``first`` : return the first `n` occurrences in order
1979+ of appearance.
1980+ - ``last`` : return the last `n` occurrences in reverse
1981+ order of appearance.
1982+ - ``all`` : keep all occurrences. This can result in a Series of
1983+ size larger than `n`.
1984+
1985+ Returns
1986+ -------
1987+ Series
1988+ The `n` smallest values in the Series, sorted in increasing order.
1989+
1990+ See Also
1991+ --------
1992+ Series.nlargest: Get the `n` largest elements.
1993+ Series.sort_values: Sort Series by values.
1994+ Series.head: Return the first `n` rows.
1995+
1996+ Notes
1997+ -----
1998+ Faster than ``.sort_values().head(n)`` for small `n` relative to
1999+ the size of the ``Series`` object.
2000+
2001+ Examples
2002+ --------
2003+ >>> countries_population = {
2004+ ... "Italy": 59000000,
2005+ ... "France": 65000000,
2006+ ... "Brunei": 434000,
2007+ ... "Malta": 434000,
2008+ ... "Maldives": 434000,
2009+ ... "Iceland": 337000,
2010+ ... "Nauru": 11300,
2011+ ... "Tuvalu": 11300,
2012+ ... "Anguilla": 11300,
2013+ ... "Montserrat": 5200,
2014+ ... }
2015+ >>> s = pd.Series(countries_population)
2016+ >>> s
2017+ Italy 59000000
2018+ France 65000000
2019+ Brunei 434000
2020+ Malta 434000
2021+ Maldives 434000
2022+ Iceland 337000
2023+ Nauru 11300
2024+ Tuvalu 11300
2025+ Anguilla 11300
2026+ Montserrat 5200
2027+ dtype: int64
2028+
2029+ The `n` smallest elements where ``n=5`` by default.
2030+
2031+ >>> s.nsmallest()
2032+ Montserrat 5200
2033+ Nauru 11300
2034+ Tuvalu 11300
2035+ Anguilla 11300
2036+ Iceland 337000
2037+ dtype: int64
2038+
2039+ The `n` smallest elements where ``n=3``. Default `keep` value is
2040+ 'first' so Nauru and Tuvalu will be kept.
2041+
2042+ >>> s.nsmallest(3)
2043+ Montserrat 5200
2044+ Nauru 11300
2045+ Tuvalu 11300
2046+ dtype: int64
2047+
2048+ The `n` smallest elements where ``n=3`` and keeping the last
2049+ duplicates. Anguilla and Tuvalu will be kept since they are the last
2050+ with value 11300 based on the index order.
2051+
2052+ >>> s.nsmallest(3, keep="last")
2053+ Montserrat 5200
2054+ Anguilla 11300
2055+ Tuvalu 11300
2056+ dtype: int64
2057+
2058+ The `n` smallest elements where ``n=3`` with all duplicates kept. Note
2059+ that the returned Series has four elements due to the three duplicates.
2060+
2061+ >>> s.nsmallest(3, keep="all")
2062+ Montserrat 5200
2063+ Nauru 11300
2064+ Tuvalu 11300
2065+ Anguilla 11300
2066+ dtype: int64
2067+ """
19682068 f = partial (Series .nsmallest , n = n , keep = keep )
19692069 data = self ._obj_with_exclusions
19702070 # Don't change behavior if result index happens to be the same, i.e.
0 commit comments