-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: _nsorted incorrect with duplicated values in index (#13412) #14707
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -684,11 +684,12 @@ def select_n_slow(dropped, n, keep, method): | |
_select_methods = {'nsmallest': nsmallest, 'nlargest': nlargest} | ||
|
||
|
||
def select_n(series, n, keep, method): | ||
"""Implement n largest/smallest. | ||
def select_n_series(series, n, keep, method): | ||
"""Implement n largest/smallest for pandas Series | ||
|
||
Parameters | ||
---------- | ||
series : pandas.Series object | ||
n : int | ||
keep : {'first', 'last'}, default 'first' | ||
method : str, {'nlargest', 'nsmallest'} | ||
|
@@ -717,6 +718,31 @@ def select_n(series, n, keep, method): | |
return dropped.iloc[inds] | ||
|
||
|
||
def select_n_frame(frame, columns, n, method, keep): | ||
"""Implement n largest/smallest for pandas DataFrame | ||
|
||
Parameters | ||
---------- | ||
series : pandas.DataFrame object | ||
columns : list or str | ||
n : int | ||
keep : {'first', 'last'}, default 'first' | ||
method : str, {'nlargest', 'nsmallest'} | ||
|
||
Returns | ||
------- | ||
nordered : DataFrame | ||
""" | ||
from pandas.core.series import Series | ||
if not is_list_like(columns): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for moving. If you are interested in next PR! would want to clean up this code w.r.t. largest/smallest a bit more anyhow; IOW, make it cleaner / nicer. (and I think we have a perf issue outstanding I think with Series). |
||
columns = [columns] | ||
columns = list(columns) | ||
ser = getattr(frame[columns[0]], method)(n, keep=keep) | ||
if isinstance(ser, Series): | ||
ser = ser.to_frame() | ||
return ser.merge(frame, on=columns[0], left_index=True)[frame.columns] | ||
|
||
|
||
def _finalize_nsmallest(arr, kth_val, n, keep, narr): | ||
ns, = np.nonzero(arr <= kth_val) | ||
inds = ns[arr[ns].argsort(kind='mergesort')][:n] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1323,6 +1323,35 @@ def test_nsmallest_multiple_columns(self): | |
expected = df.sort_values(['a', 'c']).head(5) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_nsmallest_nlargest_duplicate_index(self): | ||
# GH 13412 | ||
df = pd.DataFrame({'a': [1, 2, 3, 4], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add the issue number as a comment |
||
'b': [4, 3, 2, 1], | ||
'c': [0, 1, 2, 3]}, | ||
index=[0, 0, 1, 1]) | ||
result = df.nsmallest(4, 'a') | ||
expected = df.sort_values('a').head(4) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = df.nlargest(4, 'a') | ||
expected = df.sort_values('a', ascending=False).head(4) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = df.nsmallest(4, ['a', 'c']) | ||
expected = df.sort_values(['a', 'c']).head(4) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = df.nsmallest(4, ['c', 'a']) | ||
expected = df.sort_values(['c', 'a']).head(4) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = df.nlargest(4, ['a', 'c']) | ||
expected = df.sort_values(['a', 'c'], ascending=False).head(4) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = df.nlargest(4, ['c', 'a']) | ||
expected = df.sort_values(['c', 'a'], ascending=False).head(4) | ||
tm.assert_frame_equal(result, expected) | ||
# ---------------------------------------------------------------------- | ||
# Isin | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add doc-strings to
select_n_*
methods?. otherwise lgtm. ping on green.