From e3f666f49af3aee0a1f280e56a497c643a154c93 Mon Sep 17 00:00:00 2001 From: immerrr Date: Thu, 13 Feb 2014 17:47:18 +0400 Subject: [PATCH] PERF: speed up slice indexing on Series with string keys The patch avoids scanning the whole slice in Index.is_all_dates check. --- doc/source/release.rst | 1 + doc/source/v0.14.0.txt | 2 +- pandas/core/index.py | 4 ++-- vb_suite/indexing.py | 11 ++++++++++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 4291ed1b6c357..c188156aa5d2b 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -74,6 +74,7 @@ Improvements to existing features the func (:issue:`6289`) - ``plot(legend='reverse')`` will now reverse the order of legend labels for most plot kinds. (:issue:`6014`) +- improve performance of slice indexing on Series with string keys (:issue:`6341`) .. _release.bug_fixes-0.14.0: diff --git a/doc/source/v0.14.0.txt b/doc/source/v0.14.0.txt index ee38fed810af0..549c1156a34ea 100644 --- a/doc/source/v0.14.0.txt +++ b/doc/source/v0.14.0.txt @@ -46,7 +46,7 @@ Enhancements and parse accordingly. (:issue:`6223`) - ``plot(legend='reverse')`` will now reverse the order of legend labels for most plot kinds. (:issue:`6014`) - +- improve performance of slice indexing on Series with string keys (:issue:`6341`) Performance ~~~~~~~~~~~ diff --git a/pandas/core/index.py b/pandas/core/index.py index 5a02c0445c006..c35e61ee34fb1 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -9,7 +9,7 @@ import pandas.lib as lib import pandas.algos as _algos import pandas.index as _index -from pandas.lib import Timestamp +from pandas.lib import Timestamp, is_datetime_array from pandas.core.base import FrozenList, FrozenNDArray from pandas.util.decorators import cache_readonly, deprecate @@ -577,7 +577,7 @@ def is_type_compatible(self, typ): @cache_readonly def is_all_dates(self): - return self.inferred_type == 'datetime' + return is_datetime_array(self.values) def __iter__(self): return iter(self.values) diff --git a/vb_suite/indexing.py b/vb_suite/indexing.py index dcfda997fabd6..18838b06af756 100644 --- a/vb_suite/indexing.py +++ b/vb_suite/indexing.py @@ -7,7 +7,7 @@ """ #---------------------------------------------------------------------- -# Series.__getitem__, get_value +# Series.__getitem__, get_value, __getitem__(slice) setup = common_setup + """ tm.N = 1000 @@ -29,6 +29,15 @@ name='series_get_value', start_date=datetime(2011, 11, 12)) + +setup = common_setup + """ +index = tm.makeStringIndex(1000000) +s = Series(np.random.rand(1000000), index=index) +""" +series_getitem_slice = Benchmark("s[:800000]", setup, + name="series_getitem_slice") + + #---------------------------------------------------------------------- # DataFrame __getitem__