Skip to content

Commit a86501f

Browse files
jbrockmendeljreback
authored andcommitted
PERF: Override PeriodIndex.unique (#23083)
1 parent 4001252 commit a86501f

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

asv_bench/benchmarks/period.py

+3
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,6 @@ def time_align(self):
119119

120120
def time_intersection(self):
121121
self.index[:750].intersection(self.index[250:])
122+
123+
def time_unique(self):
124+
self.index.unique()

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ Performance Improvements
685685
(:issue:`21372`)
686686
- Improved the performance of :func:`pandas.get_dummies` with ``sparse=True`` (:issue:`21997`)
687687
- Improved performance of :func:`IndexEngine.get_indexer_non_unique` for sorted, non-unique indexes (:issue:`9466`)
688+
- Improved performance of :func:`PeriodIndex.unique` (:issue:`23083`)
688689

689690

690691
.. _whatsnew_0240.docs:

pandas/core/indexes/period.py

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
DIFFERENT_FREQ_INDEX)
3030
from pandas._libs.tslibs import resolution, period
3131

32+
from pandas.core.algorithms import unique1d
3233
from pandas.core.arrays import datetimelike as dtl
3334
from pandas.core.arrays.period import PeriodArrayMixin, dt64arr_to_periodarr
3435
from pandas.core.base import _shared_docs
@@ -539,6 +540,18 @@ def _get_unique_index(self, dropna=False):
539540
res = res.dropna()
540541
return res
541542

543+
@Appender(Index.unique.__doc__)
544+
def unique(self, level=None):
545+
# override the Index.unique method for performance GH#23083
546+
if level is not None:
547+
# this should never occur, but is retained to make the signature
548+
# match Index.unique
549+
self._validate_index_level(level)
550+
551+
values = self._ndarray_values
552+
result = unique1d(values)
553+
return self._shallow_copy(result)
554+
542555
def get_loc(self, key, method=None, tolerance=None):
543556
"""
544557
Get integer location for requested label

0 commit comments

Comments
 (0)