question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

np.unique needs an option to retain the original order

See original GitHub issue

This would integrate the work-around solutions of a number of stackoverflow questions, such as:

http://stackoverflow.com/questions/15637336/numpy-unique-with-order-preserved

['b','b','b','a','a','c','c']

numpy.unique gives ['a','b','c']

How can I get the original order preserved ['b','a','c']

The issue with these workarounds is that you can get the unique list unsorted but it is much more complicated to get a matching list with counts (from the return_counts boolian).

Having an integrated boolian (i.e. is_sorted=True) for sorting of results would clear up this issue and increase the utility of the function.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:21
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
h-vetinaricommented, Jun 21, 2018

I would need this functionality as well…

Regarding the ambiguity discussion: FWIW, pandas has implemented a “stable” (does not change order) unique, with the unambiguous choice of using either the 'first' or the 'last' occurrence of an element for determining the position in the unique output.

0reactions
dpradacommented, Nov 9, 2021

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.unique with order preserved - python - Stack Overflow
Use the return_index functionality of np.unique . That returns the indices at which the elements first occurred in the input.
Read more >
numpy.unique — NumPy v1.24 Manual
Returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements: the indices of the...
Read more >
NumPy: unique() function - w3resource
numpy.unique() function. The unique() function is used to find the unique elements of an array.Returns the sorted unique elements of an ...
Read more >
Python List: Remove Duplicates and Keep the Order - Finxter
The index values of the unique items can be stored by using the np.unique() function with the return_index parameter set to True ....
Read more >
Numpy Unique, Explained - Sharp Sight
When return_index = True , np.unique will return the index of the first occurrence of the unique value. This parameter is optional.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found