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.

Have the possibility for Series.unique() to return a Series rather than an array

See original GitHub issue

I 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:closed
  • Created 11 years ago
  • Reactions:2
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

27reactions
lazarillocommented, Jun 18, 2018

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 numpy ndarray) drop_duplicates and duplicated 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, and keep=False retains NONE of the duplicates. keep='first' is the default.)

2reactions
ddebernardycommented, Jul 26, 2019

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 references pandas.Series.drop_duplicates (and possibly vice versa).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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