Skip to content

Commit 225e9d6

Browse files
DOC: Add examples to Series.str.slice() with negative arguments GH27802
1 parent 8b6942f commit 225e9d6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pandas/core/strings.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,35 @@ def str_slice(arr, start=None, stop=None, step=None):
14541454
2 caeen
14551455
dtype: object
14561456
1457+
Negative values can be used for any of the three parameters. For start or stop, a value of -1 will be the last
1458+
character of the string, -2 the second to last, and so on. Examples:
1459+
1460+
>>> s.str.slice(start=-3)
1461+
0 ala
1462+
1 fox
1463+
2 eon
1464+
dtype: object
1465+
1466+
>>> s.str.slice(stop=-2)
1467+
0 koa
1468+
1 f
1469+
2 chamele
1470+
dtype: object
1471+
1472+
A negative value for step will simply step in the other direction.
1473+
1474+
>>> s.str.slice(step=-1)
1475+
0 alaok
1476+
1 xof
1477+
2 noelemahc
1478+
dtype: object
1479+
1480+
>>> s.str.slice(stop=1, step=-1)
1481+
0 ala
1482+
1 x
1483+
2 noelema
1484+
dtype: object
1485+
14571486
>>> s.str.slice(start=0, stop=5, step=3)
14581487
0 kl
14591488
1 f

0 commit comments

Comments
 (0)