Have the possibility for Series.unique() to return a Series rather than an array
See original GitHub issueI admit I haven’t looked at the code so there may be reasons for this, but I’ve found myself in the need of squeezing out duplicates from a Series but keeping the results as a Series.
Series.unique() however returns an array, so in my code I have to construct a Series twice:
series = pandas.Series([1,2,3,3])
series = pandas.Series(series.unique())
Is this by design? If so, feel free to close this bug.
Issue Analytics
- State:
- Created 11 years ago
- Reactions:2
- Comments:13 (10 by maintainers)
Top Results From Across the Web
pandas.Series.unique — pandas 1.5.2 documentation
pandas.Series.unique# ... Return unique values of Series object. Uniques are returned in order of appearance. Hash table-based unique, therefore does NOT sort.
Read more >Unique values from pandas.Series - python - Stack Overflow
Like in Java, and JavaScript, nan in numpy does not equal itself. ... and I would always use df.unique() to get a set...
Read more >Get Unique values in Pandas DataFrame Column - FavTutor
pandas.DataFrame().unique() method is used when we deal with a single column of a DataFrame and returns all unique elements of a column. The ......
Read more >Python Unique List – How to Get all the Unique Values in a ...
Option 1 – Using a Set to Get Unique Elements ... Using a set one way to go about it. A set is...
Read more >Pandas Series: unique() function - w3resource
The unique() function is used to get unique values of Series object. Uniques are returned in order of appearance. Hash table-based unique, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
At the risk of appearing to re-open something that is dead, I thought I’d just summarize for any newcomers to this page:
series.unique
was left unchanged (returns a numpyndarray
)drop_duplicates
andduplicated
were added to Series.So, if you wanted to do something like the OP and wanted to keep the indices, you’d perform:
series = pandas.Series([1,2,3,3]).drop_duplicates(keep='first')
(
keep='first'
retains the first occurrence of any duplicates,keep='last'
retains the last occurrence, andkeep=False
retains NONE of the duplicates.keep='first'
is the default.)In case anyone feels like writing a PR, the
pandas.Series.unique
docs doesn’t mention what @lazarillo says and could use an extra line that referencespandas.Series.drop_duplicates
(and possibly vice versa).