diff --git a/asv_bench/benchmarks/period.py b/asv_bench/benchmarks/period.py index c34f9a737473e..29b8c7efda40c 100644 --- a/asv_bench/benchmarks/period.py +++ b/asv_bench/benchmarks/period.py @@ -119,3 +119,6 @@ def time_align(self): def time_intersection(self): self.index[:750].intersection(self.index[250:]) + + def time_unique(self): + self.index.unique() diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 40dd48880e0eb..b8607136197e4 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -684,6 +684,7 @@ Performance Improvements (:issue:`21372`) - Improved the performance of :func:`pandas.get_dummies` with ``sparse=True`` (:issue:`21997`) - Improved performance of :func:`IndexEngine.get_indexer_non_unique` for sorted, non-unique indexes (:issue:`9466`) +- Improved performance of :func:`PeriodIndex.unique` (:issue:`23083`) .. _whatsnew_0240.docs: diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 7833dd851db34..f151389b02463 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -29,6 +29,7 @@ DIFFERENT_FREQ_INDEX) from pandas._libs.tslibs import resolution, period +from pandas.core.algorithms import unique1d from pandas.core.arrays import datetimelike as dtl from pandas.core.arrays.period import PeriodArrayMixin, dt64arr_to_periodarr from pandas.core.base import _shared_docs @@ -539,6 +540,18 @@ def _get_unique_index(self, dropna=False): res = res.dropna() return res + @Appender(Index.unique.__doc__) + def unique(self, level=None): + # override the Index.unique method for performance GH#23083 + if level is not None: + # this should never occur, but is retained to make the signature + # match Index.unique + self._validate_index_level(level) + + values = self._ndarray_values + result = unique1d(values) + return self._shallow_copy(result) + def get_loc(self, key, method=None, tolerance=None): """ Get integer location for requested label