Skip to content

Commit c1481af

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

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pandas/core/strings.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,36 @@ 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
1458+
value of -1 will be the last character of the string, -2 the second to last, and
1459+
so on. Examples:
1460+
1461+
>>> s.str.slice(start=-3)
1462+
0 ala
1463+
1 fox
1464+
2 eon
1465+
dtype: object
1466+
1467+
>>> s.str.slice(stop=-2)
1468+
0 koa
1469+
1 f
1470+
2 chamele
1471+
dtype: object
1472+
1473+
A negative value for step will simply step in the other direction.
1474+
1475+
>>> s.str.slice(step=-1)
1476+
0 alaok
1477+
1 xof
1478+
2 noelemahc
1479+
dtype: object
1480+
1481+
>>> s.str.slice(stop=1, step=-1)
1482+
0 ala
1483+
1 x
1484+
2 noelema
1485+
dtype: object
1486+
14571487
>>> s.str.slice(start=0, stop=5, step=3)
14581488
0 kl
14591489
1 f

0 commit comments

Comments
 (0)