-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
EHN/FIX: Add na_last parameter to DataFrame.sort. Fixes GH3917 #5231
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 |
---|---|---|
|
@@ -3145,33 +3145,72 @@ def _indexer_from_factorized(labels, shape, compress=True): | |
return indexer | ||
|
||
|
||
def _lexsort_indexer(keys, orders=None): | ||
def _lexsort_indexer(keys, orders=None, na_position='last'): | ||
labels = [] | ||
shape = [] | ||
|
||
if isinstance(orders, bool): | ||
orders = [orders] * len(keys) | ||
elif orders is None: | ||
orders = [True] * len(keys) | ||
|
||
for key, order in zip(keys, orders): | ||
key = np.asanyarray(key) | ||
rizer = _hash.Factorizer(len(key)) | ||
|
||
if not key.dtype == np.object_: | ||
key = key.astype('O') | ||
|
||
# factorize maps nans to na_sentinel=-1 | ||
ids = rizer.factorize(key, sort=True) | ||
|
||
n = len(rizer.uniques) | ||
mask = (ids == -1) | ||
if order: # ascending | ||
if na_position == 'last': | ||
ids = np.where(mask, n, ids) | ||
elif na_position == 'first': | ||
ids += 1 | ||
else: | ||
raise ValueError('invalid na_position: {!r}'.format(na_position)) | ||
else: # not order means descending | ||
if na_position == 'last': | ||
ids = np.where(mask, n, n-ids-1) | ||
elif na_position == 'first': | ||
ids = np.where(mask, 0, n-ids) | ||
else: | ||
raise ValueError('invalid na_position: {!r}'.format(na_position)) | ||
if mask.any(): | ||
n += 1 | ||
shape.append(n) | ||
if not order: | ||
mask = ids == -1 | ||
ids = np.where(mask, -1, n - ids) | ||
|
||
labels.append(ids) | ||
|
||
return _indexer_from_factorized(labels, shape) | ||
|
||
def _nargsort(items, kind='quicksort', ascending=True, na_position='last'): | ||
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. move to 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. Do you mean core/algorithms.py? or algos.pyx? 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. yes...that's what I meant! 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. wait...nvm....I realize this is being called internall in groupby.py |
||
""" | ||
This is intended to be a drop-in replacement for np.argsort which handles NaNs | ||
It adds ascending and na_position parameters. | ||
GH #6399, #5231 | ||
""" | ||
items = np.asanyarray(items) | ||
idx = np.arange(len(items)) | ||
mask = isnull(items) | ||
non_nans = items[~mask] | ||
non_nan_idx = idx[~mask] | ||
nan_idx = np.nonzero(mask)[0] | ||
if not ascending: | ||
non_nans = non_nans[::-1] | ||
non_nan_idx = non_nan_idx[::-1] | ||
indexer = non_nan_idx[non_nans.argsort(kind=kind)] | ||
if not ascending: | ||
indexer = indexer[::-1] | ||
# Finally, place the NaNs at the end or the beginning according to na_position | ||
if na_position == 'last': | ||
indexer = np.concatenate([indexer, nan_idx]) | ||
elif na_position == 'first': | ||
indexer = np.concatenate([nan_idx, indexer]) | ||
else: | ||
raise ValueError('invalid na_position: {!r}'.format(na_position)) | ||
return indexer | ||
|
||
|
||
class _KeyMapper(object): | ||
|
||
|
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.
not sure why
_lexsort_indexer
is even in groupby.py only uses in series/frame. can you move toalgos.py
?