Skip to content

Commit f2087df

Browse files
committed
BUG: Implement step in slice StringMethod
Resolves pandas-dev#8754.
1 parent 0f899f4 commit f2087df

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

doc/source/whatsnew/v0.15.2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Bug Fixes
6969
- ``io.data.Options`` now raises ``RemoteDataError`` when no expiry dates are available from Yahoo (:issue:`8761`).
7070
- ``Timedelta`` kwargs may now be numpy ints and floats (:issue:`8757`).
7171
- ``sql_schema`` now generates dialect appropriate ``CREATE TABLE`` statements (:issue:`8697`)
72+
- ``slice`` string method now takes step into account (:issue:`8754`)
7273

7374

7475

pandas/core/strings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ def str_slice(arr, start=None, stop=None, step=1):
674674
----------
675675
start : int or None
676676
stop : int or None
677+
step : int or None
677678
678679
Returns
679680
-------
@@ -994,7 +995,7 @@ def center(self, width):
994995

995996
@copy(str_slice)
996997
def slice(self, start=None, stop=None, step=1):
997-
result = str_slice(self.series, start, stop)
998+
result = str_slice(self.series, start, stop, step)
998999
return self._wrap_result(result)
9991000

10001001
@copy(str_slice)

pandas/tests/test_strings.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ def test_empty_str_methods(self):
628628
tm.assert_series_equal(empty_str, empty.str.center(42))
629629
tm.assert_series_equal(empty_list, empty.str.split('a'))
630630
tm.assert_series_equal(empty_str, empty.str.slice(stop=1))
631+
tm.assert_series_equal(empty_str, empty.str.slice(step=1))
631632
tm.assert_series_equal(empty_str, empty.str.strip())
632633
tm.assert_series_equal(empty_str, empty.str.lstrip())
633634
tm.assert_series_equal(empty_str, empty.str.rstrip())
@@ -922,6 +923,17 @@ def test_slice(self):
922923
exp = Series(['foo', 'bar', NA, 'baz'])
923924
tm.assert_series_equal(result, exp)
924925

926+
for start, stop, step in [(0, 3, -1), (None, None, -1),
927+
(3, 10, 2), (3, 0, -1)]:
928+
try:
929+
result = values.str.slice(start, stop, step)
930+
expected = Series([s[start:stop:step] if not isnull(s) else NA for s in
931+
values])
932+
tm.assert_series_equal(result, expected)
933+
except:
934+
print('failed on %s:%s:%s' % (start, stop, step))
935+
raise
936+
925937
# mixed
926938
mixed = Series(['aafootwo', NA, 'aabartwo', True, datetime.today(),
927939
None, 1, 2.])
@@ -933,6 +945,10 @@ def test_slice(self):
933945
tm.assert_isinstance(rs, Series)
934946
tm.assert_almost_equal(rs, xp)
935947

948+
rs = Series(mixed).str.slice(2, 5, -1)
949+
xp = Series(['oof', NA, 'rab', NA, NA,
950+
NA, NA, NA])
951+
936952
# unicode
937953
values = Series([u('aafootwo'), u('aabartwo'), NA,
938954
u('aabazqux')])
@@ -941,6 +957,10 @@ def test_slice(self):
941957
exp = Series([u('foo'), u('bar'), NA, u('baz')])
942958
tm.assert_series_equal(result, exp)
943959

960+
result = values.str.slice(0, -1, 2)
961+
exp = Series([u('afow'), u('abrw'), NA, u('abzu')])
962+
tm.assert_series_equal(result, exp)
963+
944964
def test_slice_replace(self):
945965
pass
946966

0 commit comments

Comments
 (0)