MCPcopy Create free account
hub / github.com/numpy/numpy / replace

Function replace

numpy/core/defchararray.py:1242–1279  ·  view source on GitHub ↗

For each element in `a`, return a copy of the string with all occurrences of substring `old` replaced by `new`. Calls `str.replace` element-wise. Parameters ---------- a : array-like of str or unicode old, new : str or unicode count : int, optional If the

(a, old, new, count=None)

Source from the content-addressed store, hash-verified

1240
1241@array_function_dispatch(_replace_dispatcher)
1242def replace(a, old, new, count=None):
1243 """
1244 For each element in `a`, return a copy of the string with all
1245 occurrences of substring `old` replaced by `new`.
1246
1247 Calls `str.replace` element-wise.
1248
1249 Parameters
1250 ----------
1251 a : array-like of str or unicode
1252
1253 old, new : str or unicode
1254
1255 count : int, optional
1256 If the optional argument `count` is given, only the first
1257 `count` occurrences are replaced.
1258
1259 Returns
1260 -------
1261 out : ndarray
1262 Output array of str or unicode, depending on input type
1263
1264 See Also
1265 --------
1266 str.replace
1267
1268 Examples
1269 --------
1270 >>> a = np.array(["That is a mango", "Monkeys eat mangos"])
1271 >>> np.char.replace(a, 'mango', 'banana')
1272 array(['That is a banana', 'Monkeys eat bananas'], dtype='<U19')
1273
1274 >>> a = np.array(["The dish is fresh", "This is it"])
1275 >>> np.char.replace(a, 'is', 'was')
1276 array(['The dwash was fresh', 'Thwas was it'], dtype='<U19')
1277 """
1278 return _to_bytes_or_str_array(
1279 _vec_string(a, object_, 'replace', [old, new] + _clean_args(count)), a)
1280
1281
1282@array_function_dispatch(_count_dispatcher)

Callers 1

replaceMethod · 0.70

Calls 3

_vec_stringFunction · 0.90
_to_bytes_or_str_arrayFunction · 0.85
_clean_argsFunction · 0.85

Tested by

no test coverage detected