Skip to content

Commit ca8c00e

Browse files
committed
PERF: fix series slice indexing performance
1 parent b596852 commit ca8c00e

File tree

4 files changed

+14
-23
lines changed

4 files changed

+14
-23
lines changed

doc/source/release.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Improvements to existing features
7979
- ``plot(legend='reverse')`` will now reverse the order of legend labels for most plot kinds.
8080
(:issue:`6014`)
8181
- Allow multi-index slicers (:issue:`6134`, :issue:`4036`, :issue:`3057`, :issue:`2598`, :issue:`5641`)
82-
- improve performance of slice indexing on Series with string keys (:issue:`6341`)
82+
- improve performance of slice indexing on Series with string keys (:issue:`6341`, :issue:`6372`)
8383
- implement joining a single-level indexed DataFrame on a matching column of a multi-indexed DataFrame (:issue:`3662`)
8484
- Performance improvement in indexing into a multi-indexed Series (:issue:`5567`)
8585
- Testing statements updated to use specialized asserts (:issue: `6175`)

doc/source/v0.14.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Enhancements
153153
and parse accordingly. (:issue:`6223`)
154154
- ``plot(legend='reverse')`` will now reverse the order of legend labels for
155155
most plot kinds. (:issue:`6014`)
156-
- improve performance of slice indexing on Series with string keys (:issue:`6341`)
156+
- improve performance of slice indexing on Series with string keys (:issue:`6341`, :issue:`6372`)
157157
- Hexagonal bin plots from ``DataFrame.plot`` with ``kind='hexbin'`` (:issue:`5478`)
158158
- Joining a singly-indexed DataFrame with a multi-indexed DataFrame (:issue:`3662`)
159159

pandas/core/series.py

+1-19
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,7 @@ def _slice(self, slobj, axis=0, raise_on_error=False, typ=None):
463463
if raise_on_error:
464464
_check_slice_bounds(slobj, self.values)
465465
slobj = self.index._convert_slice_indexer(slobj, typ=typ or 'getitem')
466-
return self._constructor(self.values[slobj],
467-
index=self.index[slobj]).__finalize__(self)
466+
return self._get_values(slobj)
468467

469468
def __getitem__(self, key):
470469
try:
@@ -679,23 +678,6 @@ def _set_values(self, key, value):
679678
# help out SparseSeries
680679
_get_val_at = ndarray.__getitem__
681680

682-
def __getslice__(self, i, j):
683-
if i < 0:
684-
i = 0
685-
if j < 0:
686-
j = 0
687-
slobj = slice(i, j)
688-
return self._slice(slobj)
689-
690-
def __setslice__(self, i, j, value):
691-
"""Set slice equal to given value(s)"""
692-
if i < 0:
693-
i = 0
694-
if j < 0:
695-
j = 0
696-
slobj = slice(i, j)
697-
return self.__setitem__(slobj, value)
698-
699681
def repeat(self, reps):
700682
"""
701683
See ndarray.repeat

vb_suite/indexing.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@
3434
index = tm.makeStringIndex(1000000)
3535
s = Series(np.random.rand(1000000), index=index)
3636
"""
37-
series_getitem_slice = Benchmark("s[:800000]", setup,
38-
name="series_getitem_slice")
37+
series_getitem_pos_slice = Benchmark("s[:800000]", setup,
38+
name="series_getitem_pos_slice")
39+
40+
41+
setup = common_setup + """
42+
index = tm.makeStringIndex(1000000)
43+
s = Series(np.random.rand(1000000), index=index)
44+
lbl = s.index[800000]
45+
"""
46+
series_getitem_label_slice = Benchmark("s[:lbl]", setup,
47+
name="series_getitem_label_slice")
3948

4049

4150
#----------------------------------------------------------------------

0 commit comments

Comments
 (0)