Skip to content

Commit

Permalink
Merge pull request #123 from brentomagic/unique
Browse files Browse the repository at this point in the history
unique()
  • Loading branch information
MarcoGorelli authored May 9, 2024
2 parents a41ac90 + fed84c4 commit 270b1d0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions narwhals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,40 @@ def cum_sum(self) -> Self:
return self._from_series(self._series.cum_sum())

def unique(self) -> Self:
"""
Returns unique values
Examples:
>>> import pandas as pd
>>> import polars as pl
>>> import narwhals as nw
>>> s = [2, 4, 4, 6]
>>> s_pd = pd.Series(s)
>>> s_pl = pl.Series(s)
Let's define a dataframe-agnostic function:
>>> def func(s_any):
... s = nw.from_native(s_any, series_only=True)
... s = s.unique()
... return nw.to_native(s)
We can then pass either pandas or Polars to `func`:
>>> func(s_pd)
0 2
1 4
2 6
dtype: int64
>>> func(s_pl) # doctest: +NORMALIZE_WHITESPACE
shape: (3,)
Series: '' [i64]
[
2
4
6
]
"""
return self._from_series(self._series.unique())

def diff(self) -> Self:
Expand Down

0 comments on commit 270b1d0

Please sign in to comment.