Skip to content

Commit bbed286

Browse files
committed
Updated
1 parent 916f4cb commit bbed286

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

pandas/core/strings.py

+56-21
Original file line numberDiff line numberDiff line change
@@ -1178,42 +1178,77 @@ def str_slice(arr, start=None, stop=None, step=None):
11781178
return _na_map(f, arr)
11791179

11801180

1181-
def str_slice_replace(arr, start=None, stop=None, repl=None):
1181+
def str_slice_replace(arr, start=None, stop=None, repl=''):
11821182
"""
1183-
Replace a sliced string.
1184-
1185-
Replace a slice of each string in the Series/Index with another
1186-
string.
1183+
Replace a positional slice of a string with another value.
11871184
11881185
Parameters
11891186
----------
1190-
start : int or None
1191-
Left edge index.
1192-
stop : int or None
1193-
Right edge index.
1194-
repl : str or None
1187+
start : int, optional
1188+
Left index position to use for the slice. The default of None
1189+
implies a slice unbound on the left, i.e. slice from the start
1190+
of the string.
1191+
stop : int, optional
1192+
Right index position to use for the slice. The default of None
1193+
implies a slice unbounded on the right, i.e. slice until the
1194+
end of the string.
1195+
repl : str, default ''
11951196
String for replacement.
11961197
11971198
Returns
11981199
-------
1199-
replaced : Series/Index of objects
1200+
replaced : Series or Index
1201+
Same type as the original object.
1202+
1203+
See Also
1204+
--------
1205+
Series.str.slice : Just slicing without replacement.
12001206
12011207
Examples
12021208
--------
1203-
>>> s = pd.Series(['This is a Test 1', 'This is a Test 2'])
1209+
>>> s = pd.Series(['a', 'ab', 'abc', 'abdc', 'abcde'])
12041210
>>> s
1205-
0 This is a Test 1
1206-
1 This is a Test 2
1211+
0 a
1212+
1 ab
1213+
2 abc
1214+
3 abdc
1215+
4 abcde
12071216
dtype: object
1208-
>>> s = s.str.slice_replace(8, 14, 'an Example')
1209-
>>> s
1210-
0 This is an Example 1
1211-
1 This is an Example 2
1217+
1218+
Specify just `start`, meaning replace `start` until the end of the
1219+
string with `repl`.
1220+
1221+
>>> s.str.slice_replace(1, repl='X')
1222+
0 aX
1223+
1 aX
1224+
2 aX
1225+
3 aX
1226+
4 aX
12121227
dtype: object
1213-
"""
1214-
if repl is None:
1215-
repl = ''
12161228
1229+
Specify just `stop`, meaning the start of the string to `stop` is replaced
1230+
with `repl`, and the rest of the string is included.
1231+
1232+
>>> s.str.slice_replace(stop=2, repl='X')
1233+
0 X
1234+
1 X
1235+
2 Xc
1236+
3 Xdc
1237+
4 Xcde
1238+
dtype: object
1239+
1240+
Specify `start` and `stop`, meaning the slice from `start` to `stop` is
1241+
replaced with `repl`. Everything before or after `start` and `stop` is
1242+
included as is.
1243+
1244+
>>> s.str.slice_replace(start=1, stop=3, repl='X')
1245+
0 aX
1246+
1 aX
1247+
2 aX
1248+
3 aXc
1249+
4 aXde
1250+
dtype: object
1251+
"""
12171252
def f(x):
12181253
if x[start:stop] == '':
12191254
local_stop = start

0 commit comments

Comments
 (0)