From f8d170bbae5d53f25900ce800d5cac6936d3bf16 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 10 Oct 2018 18:57:23 -0700 Subject: [PATCH 1/3] PERF: Override PeriodIndex.unique --- pandas/core/indexes/period.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 7833dd851db34..218abb5dc8a40 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 + 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 From bf96253a63f0e06b94548e59ee43f4bac52054bd Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 10 Oct 2018 19:08:53 -0700 Subject: [PATCH 2/3] add asv --- asv_bench/benchmarks/period.py | 3 +++ 1 file changed, 3 insertions(+) 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() From 3bd918c2a4052a2a10e8916c67baad6c2b19f4eb Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 10 Oct 2018 20:22:21 -0700 Subject: [PATCH 3/3] whatsnew performance note --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/core/indexes/period.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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 218abb5dc8a40..f151389b02463 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -542,7 +542,7 @@ def _get_unique_index(self, dropna=False): @Appender(Index.unique.__doc__) def unique(self, level=None): - # override the Index.unique method for performance + # 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