Skip to content

Commit e3f666f

Browse files
committed
PERF: speed up slice indexing on Series with string keys
The patch avoids scanning the whole slice in Index.is_all_dates check.
1 parent 87b4308 commit e3f666f

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Improvements to existing features
7474
the func (:issue:`6289`)
7575
- ``plot(legend='reverse')`` will now reverse the order of legend labels for most plot kinds.
7676
(:issue:`6014`)
77+
- improve performance of slice indexing on Series with string keys (:issue:`6341`)
7778

7879
.. _release.bug_fixes-0.14.0:
7980

doc/source/v0.14.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Enhancements
4646
and parse accordingly. (:issue:`6223`)
4747
- ``plot(legend='reverse')`` will now reverse the order of legend labels for
4848
most plot kinds. (:issue:`6014`)
49-
49+
- improve performance of slice indexing on Series with string keys (:issue:`6341`)
5050

5151
Performance
5252
~~~~~~~~~~~

pandas/core/index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pandas.lib as lib
1010
import pandas.algos as _algos
1111
import pandas.index as _index
12-
from pandas.lib import Timestamp
12+
from pandas.lib import Timestamp, is_datetime_array
1313
from pandas.core.base import FrozenList, FrozenNDArray
1414

1515
from pandas.util.decorators import cache_readonly, deprecate
@@ -577,7 +577,7 @@ def is_type_compatible(self, typ):
577577

578578
@cache_readonly
579579
def is_all_dates(self):
580-
return self.inferred_type == 'datetime'
580+
return is_datetime_array(self.values)
581581

582582
def __iter__(self):
583583
return iter(self.values)

vb_suite/indexing.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
#----------------------------------------------------------------------
10-
# Series.__getitem__, get_value
10+
# Series.__getitem__, get_value, __getitem__(slice)
1111

1212
setup = common_setup + """
1313
tm.N = 1000
@@ -29,6 +29,15 @@
2929
name='series_get_value',
3030
start_date=datetime(2011, 11, 12))
3131

32+
33+
setup = common_setup + """
34+
index = tm.makeStringIndex(1000000)
35+
s = Series(np.random.rand(1000000), index=index)
36+
"""
37+
series_getitem_slice = Benchmark("s[:800000]", setup,
38+
name="series_getitem_slice")
39+
40+
3241
#----------------------------------------------------------------------
3342
# DataFrame __getitem__
3443

0 commit comments

Comments
 (0)