@@ -1178,42 +1178,77 @@ def str_slice(arr, start=None, stop=None, step=None):
1178
1178
return _na_map (f , arr )
1179
1179
1180
1180
1181
- def str_slice_replace (arr , start = None , stop = None , repl = None ):
1181
+ def str_slice_replace (arr , start = None , stop = None , repl = '' ):
1182
1182
"""
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.
1187
1184
1188
1185
Parameters
1189
1186
----------
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 ''
1195
1196
String for replacement.
1196
1197
1197
1198
Returns
1198
1199
-------
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.
1200
1206
1201
1207
Examples
1202
1208
--------
1203
- >>> s = pd.Series(['This is a Test 1' , 'This is a Test 2 '])
1209
+ >>> s = pd.Series(['a', 'ab', 'abc' , 'abdc', 'abcde '])
1204
1210
>>> 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
1207
1216
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
1212
1227
dtype: object
1213
- """
1214
- if repl is None :
1215
- repl = ''
1216
1228
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
+ """
1217
1252
def f (x ):
1218
1253
if x [start :stop ] == '' :
1219
1254
local_stop = start
0 commit comments