Skip to content
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

ENH: Allow keep='all' for nlargest/nsmallest #21650

Merged
merged 1 commit into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asv_bench/benchmarks/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def time_info(self):
class NSort(object):

goal_time = 0.2
params = ['first', 'last']
params = ['first', 'last', 'all']
param_names = ['keep']

def setup(self, keep):
Expand Down
2 changes: 1 addition & 1 deletion asv_bench/benchmarks/series_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def time_isin(self, dtypes):
class NSort(object):

goal_time = 0.2
params = ['last', 'first']
params = ['first', 'last', 'all']
param_names = ['keep']

def setup(self, keep):
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Other Enhancements
<https://pandas-gbq.readthedocs.io/en/latest/changelog.html#changelog-0-5-0>`__.
(:issue:`21627`)
- New method :meth:`HDFStore.walk` will recursively walk the group hierarchy of an HDF5 file (:issue:`10932`)
- :meth:`Series.nlargest`, :meth:`Series.nsmallest`, :meth:`DataFrame.nlargest`, and :meth:`DataFrame.nsmallest` now accept the value ``"all"`` for the ``keep` argument. This keeps all ties for the nth largest/smallest value (:issue:`16818`)
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 both issue numbers here?

Copy link
Member Author

Choose a reason for hiding this comment

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

The second issue number #18656 is an actually the old PR. I mentioned it for completeness.

-

.. _whatsnew_0240.api_breaking:
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,8 +1076,8 @@ def __init__(self, obj, n, keep):
self.n = n
self.keep = keep

if self.keep not in ('first', 'last'):
raise ValueError('keep must be either "first", "last"')
if self.keep not in ('first', 'last', 'all'):
raise ValueError('keep must be either "first", "last" or "all"')

def nlargest(self):
return self.compute('nlargest')
Expand Down Expand Up @@ -1148,7 +1148,11 @@ def compute(self, method):

kth_val = algos.kth_smallest(arr.copy(), n - 1)
ns, = np.nonzero(arr <= kth_val)
inds = ns[arr[ns].argsort(kind='mergesort')][:n]
inds = ns[arr[ns].argsort(kind='mergesort')]

if self.keep != 'all':
inds = inds[:n]

if self.keep == 'last':
# reverse indices
inds = narr - 1 - inds
Expand Down
101 changes: 83 additions & 18 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4559,11 +4559,15 @@ def nlargest(self, n, columns, keep='first'):
Number of rows to return.
columns : label or list of labels
Column label(s) to order by.
keep : {'first', 'last'}, default 'first'
keep : {'first', 'last', 'all'}, default 'first'
Where there are duplicate values:

- `first` : prioritize the first occurrence(s)
- `last` : prioritize the last occurrence(s)
- ``all`` : do not drop any duplicates, even it means
selecting more than `n` items.

.. versionadded:: 0.24.0

Returns
-------
Expand All @@ -4586,47 +4590,58 @@ def nlargest(self, n, columns, keep='first'):

Examples
--------
>>> df = pd.DataFrame({'a': [1, 10, 8, 10, -1],
... 'b': list('abdce'),
... 'c': [1.0, 2.0, np.nan, 3.0, 4.0]})
>>> df = pd.DataFrame({'a': [1, 10, 8, 11, 8, 2],
... 'b': list('abdcef'),
... 'c': [1.0, 2.0, np.nan, 3.0, 4.0, 9.0]})
>>> df
a b c
0 1 a 1.0
1 10 b 2.0
2 8 d NaN
3 10 c 3.0
4 -1 e 4.0
3 11 c 3.0
4 8 e 4.0
5 2 f 9.0

In the following example, we will use ``nlargest`` to select the three
rows having the largest values in column "a".

>>> df.nlargest(3, 'a')
a b c
3 11 c 3.0
1 10 b 2.0
3 10 c 3.0
2 8 d NaN

When using ``keep='last'``, ties are resolved in reverse order:

>>> df.nlargest(3, 'a', keep='last')
a b c
3 10 c 3.0
3 11 c 3.0
1 10 b 2.0
4 8 e 4.0

When using ``keep='all'``, all duplicate items are maintained:

>>> df.nlargest(3, 'a', keep='all')
a b c
3 11 c 3.0
1 10 b 2.0
2 8 d NaN
4 8 e 4.0

To order by the largest values in column "a" and then "c", we can
specify multiple columns like in the next example.

>>> df.nlargest(3, ['a', 'c'])
a b c
3 10 c 3.0
4 8 e 4.0
3 11 c 3.0
1 10 b 2.0
2 8 d NaN

Attempting to use ``nlargest`` on non-numeric dtypes will raise a
``TypeError``:

>>> df.nlargest(3, 'b')

Traceback (most recent call last):
TypeError: Column 'b' has dtype object, cannot use method 'nlargest'
"""
Expand All @@ -4645,25 +4660,75 @@ def nsmallest(self, n, columns, keep='first'):
Number of items to retrieve
columns : list or str
Column name or names to order by
keep : {'first', 'last'}, default 'first'
keep : {'first', 'last', 'all'}, default 'first'
Where there are duplicate values:
- ``first`` : take the first occurrence.
- ``last`` : take the last occurrence.
- ``all`` : do not drop any duplicates, even it means
selecting more than `n` items.

.. versionadded:: 0.24.0

Returns
-------
DataFrame

Examples
--------
>>> df = pd.DataFrame({'a': [1, 10, 8, 11, -1],
... 'b': list('abdce'),
... 'c': [1.0, 2.0, np.nan, 3.0, 4.0]})
>>> df = pd.DataFrame({'a': [1, 10, 8, 11, 8, 2],
... 'b': list('abdcef'),
... 'c': [1.0, 2.0, np.nan, 3.0, 4.0, 9.0]})
>>> df
a b c
0 1 a 1.0
1 10 b 2.0
2 8 d NaN
3 11 c 3.0
4 8 e 4.0
5 2 f 9.0

In the following example, we will use ``nsmallest`` to select the
three rows having the smallest values in column "a".

>>> df.nsmallest(3, 'a')
a b c
4 -1 e 4
0 1 a 1
2 8 d NaN
a b c
0 1 a 1.0
5 2 f 9.0
2 8 d NaN

When using ``keep='last'``, ties are resolved in reverse order:

>>> df.nsmallest(3, 'a', keep='last')
a b c
0 1 a 1.0
5 2 f 9.0
4 8 e 4.0

When using ``keep='all'``, all duplicate items are maintained:

>>> df.nsmallest(3, 'a', keep='all')
a b c
0 1 a 1.0
5 2 f 9.0
2 8 d NaN
4 8 e 4.0

To order by the largest values in column "a" and then "c", we can
specify multiple columns like in the next example.

>>> df.nsmallest(3, ['a', 'c'])
a b c
0 1 a 1.0
5 2 f 9.0
4 8 e 4.0

Attempting to use ``nsmallest`` on non-numeric dtypes will raise a
``TypeError``:

>>> df.nsmallest(3, 'b')

Traceback (most recent call last):
TypeError: Column 'b' has dtype object, cannot use method 'nsmallest'
"""
return algorithms.SelectNFrame(self,
n=n,
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,22 @@ def test_n_duplicate_index(self, df_duplicates, n, order):
expected = df.sort_values(order, ascending=False).head(n)
tm.assert_frame_equal(result, expected)

def test_duplicate_keep_all_ties(self):
# see gh-16818
df = pd.DataFrame({'a': [5, 4, 4, 2, 3, 3, 3, 3],
'b': [10, 9, 8, 7, 5, 50, 10, 20]})
result = df.nlargest(4, 'a', keep='all')
expected = pd.DataFrame({'a': {0: 5, 1: 4, 2: 4, 4: 3,
5: 3, 6: 3, 7: 3},
'b': {0: 10, 1: 9, 2: 8, 4: 5,
5: 50, 6: 10, 7: 20}})
tm.assert_frame_equal(result, expected)

result = df.nsmallest(2, 'a', keep='all')
expected = pd.DataFrame({'a': {3: 2, 4: 3, 5: 3, 6: 3, 7: 3},
'b': {3: 7, 4: 5, 5: 50, 6: 10, 7: 20}})
tm.assert_frame_equal(result, expected)

def test_series_broadcasting(self):
# smoke test for numpy warnings
# GH 16378, GH 16306
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,17 @@ def test_boundary_datetimelike(self, nselect_method, dtype):
vals = [min_val + 1, min_val + 2, max_val - 1, max_val, min_val]
assert_check_nselect_boundary(vals, dtype, nselect_method)

def test_duplicate_keep_all_ties(self):
# see gh-16818
s = Series([10, 9, 8, 7, 7, 7, 7, 6])
result = s.nlargest(4, keep='all')
expected = Series([10, 9, 8, 7, 7, 7, 7])
assert_series_equal(result, expected)

result = s.nsmallest(2, keep='all')
expected = Series([6, 7, 7, 7, 7], index=[7, 3, 4, 5, 6])
assert_series_equal(result, expected)


class TestCategoricalSeriesAnalytics(object):

Expand Down