|
6 | 6 | # pylint: disable=E1101,E1103 |
7 | 7 | # pylint: disable=W0703,W0622,W0613,W0201 |
8 | 8 |
|
9 | | -import operator |
10 | 9 | import types |
11 | 10 | import warnings |
12 | 11 |
|
|
15 | 14 | import numpy.ma as ma |
16 | 15 |
|
17 | 16 | from pandas.core.common import (isnull, notnull, _is_bool_indexer, |
18 | | - _default_index, _maybe_promote, _maybe_upcast, |
19 | | - _asarray_tuplesafe, is_integer_dtype, |
20 | | - _NS_DTYPE, _TD_DTYPE, |
21 | | - _infer_dtype_from_scalar, is_list_like, |
22 | | - _values_from_object, |
| 17 | + _default_index, _maybe_upcast, |
| 18 | + _asarray_tuplesafe, _infer_dtype_from_scalar, |
| 19 | + is_list_like, _values_from_object, |
23 | 20 | _possibly_cast_to_datetime, _possibly_castable, |
24 | | - _possibly_convert_platform, |
25 | | - _try_sort, |
| 21 | + _possibly_convert_platform, _try_sort, |
26 | 22 | ABCSparseArray, _maybe_match_name, |
27 | 23 | _ensure_object, SettingWithCopyError) |
28 | 24 | from pandas.core.index import (Index, MultiIndex, InvalidIndexError, |
29 | 25 | _ensure_index) |
30 | | -from pandas.core.indexing import ( |
31 | | - _check_bool_indexer, |
32 | | - _is_index_slice, _maybe_convert_indices) |
| 26 | +from pandas.core.indexing import _check_bool_indexer, _maybe_convert_indices |
33 | 27 | from pandas.core import generic, base |
34 | 28 | from pandas.core.internals import SingleBlockManager |
35 | 29 | from pandas.core.categorical import Categorical |
36 | 30 | from pandas.tseries.index import DatetimeIndex |
37 | 31 | from pandas.tseries.period import PeriodIndex, Period |
38 | 32 | from pandas import compat |
39 | 33 | from pandas.util.terminal import get_terminal_size |
40 | | -from pandas.compat import zip, lzip, u, OrderedDict |
| 34 | +from pandas.compat import zip, u, OrderedDict |
41 | 35 |
|
42 | 36 | import pandas.core.array as pa |
43 | 37 | import pandas.core.ops as ops |
| 38 | +from pandas.core.algorithms import select_n |
44 | 39 |
|
45 | 40 | import pandas.core.common as com |
46 | 41 | import pandas.core.datetools as datetools |
47 | 42 | import pandas.core.format as fmt |
48 | 43 | import pandas.core.nanops as nanops |
49 | | -from pandas.util.decorators import Appender, Substitution, cache_readonly |
| 44 | +from pandas.util.decorators import Appender, cache_readonly |
50 | 45 |
|
51 | 46 | import pandas.lib as lib |
52 | 47 | import pandas.tslib as tslib |
@@ -1728,6 +1723,72 @@ def _try_kind_sort(arr): |
1728 | 1723 | else: |
1729 | 1724 | return result.__finalize__(self) |
1730 | 1725 |
|
| 1726 | + def nlargest(self, n=5, take_last=False): |
| 1727 | + """Return the largest `n` elements. |
| 1728 | +
|
| 1729 | + Parameters |
| 1730 | + ---------- |
| 1731 | + n : int |
| 1732 | + Return this many descending sorted values |
| 1733 | + take_last : bool |
| 1734 | + Where there are duplicate values, take the last duplicate |
| 1735 | +
|
| 1736 | + Returns |
| 1737 | + ------- |
| 1738 | + top_n : Series |
| 1739 | + The n largest values in the Series, in sorted order |
| 1740 | +
|
| 1741 | + Notes |
| 1742 | + ----- |
| 1743 | + Faster than ``.order(ascending=False).head(n)`` for small `n` relative |
| 1744 | + to the size of the ``Series`` object. |
| 1745 | +
|
| 1746 | + See Also |
| 1747 | + -------- |
| 1748 | + Series.nsmallest |
| 1749 | +
|
| 1750 | + Examples |
| 1751 | + -------- |
| 1752 | + >>> import pandas as pd |
| 1753 | + >>> import numpy as np |
| 1754 | + >>> s = pd.Series(np.random.randn(1e6)) |
| 1755 | + >>> s.nlargest(10) # only sorts up to the N requested |
| 1756 | + """ |
| 1757 | + return select_n(self, n=n, take_last=take_last, method='nlargest') |
| 1758 | + |
| 1759 | + def nsmallest(self, n=5, take_last=False): |
| 1760 | + """Return the smallest `n` elements. |
| 1761 | +
|
| 1762 | + Parameters |
| 1763 | + ---------- |
| 1764 | + n : int |
| 1765 | + Return this many ascending sorted values |
| 1766 | + take_last : bool |
| 1767 | + Where there are duplicate values, take the last duplicate |
| 1768 | +
|
| 1769 | + Returns |
| 1770 | + ------- |
| 1771 | + bottom_n : Series |
| 1772 | + The n smallest values in the Series, in sorted order |
| 1773 | +
|
| 1774 | + Notes |
| 1775 | + ----- |
| 1776 | + Faster than ``.order().head(n)`` for small `n` relative to |
| 1777 | + the size of the ``Series`` object. |
| 1778 | +
|
| 1779 | + See Also |
| 1780 | + -------- |
| 1781 | + Series.nlargest |
| 1782 | +
|
| 1783 | + Examples |
| 1784 | + -------- |
| 1785 | + >>> import pandas as pd |
| 1786 | + >>> import numpy as np |
| 1787 | + >>> s = pd.Series(np.random.randn(1e6)) |
| 1788 | + >>> s.nsmallest(10) # only sorts up to the N requested |
| 1789 | + """ |
| 1790 | + return select_n(self, n=n, take_last=take_last, method='nsmallest') |
| 1791 | + |
1731 | 1792 | def sortlevel(self, level=0, ascending=True, sort_remaining=True): |
1732 | 1793 | """ |
1733 | 1794 | Sort Series with MultiIndex by chosen level. Data will be |
|
0 commit comments