Return element-wise string concatenation for two arrays of str or unicode. Arrays `x1` and `x2` must have the same shape. Parameters ---------- x1 : array_like of str or unicode Input array. x2 : array_like of str or unicode Input array. Returns --
(x1, x2)
| 300 | |
| 301 | @array_function_dispatch(_binary_op_dispatcher) |
| 302 | def add(x1, x2): |
| 303 | """ |
| 304 | Return element-wise string concatenation for two arrays of str or unicode. |
| 305 | |
| 306 | Arrays `x1` and `x2` must have the same shape. |
| 307 | |
| 308 | Parameters |
| 309 | ---------- |
| 310 | x1 : array_like of str or unicode |
| 311 | Input array. |
| 312 | x2 : array_like of str or unicode |
| 313 | Input array. |
| 314 | |
| 315 | Returns |
| 316 | ------- |
| 317 | add : ndarray |
| 318 | Output array of `bytes_` or `str_`, depending on input types |
| 319 | of the same shape as `x1` and `x2`. |
| 320 | |
| 321 | """ |
| 322 | arr1 = numpy.asarray(x1) |
| 323 | arr2 = numpy.asarray(x2) |
| 324 | out_size = _get_num_chars(arr1) + _get_num_chars(arr2) |
| 325 | |
| 326 | if type(arr1.dtype) != type(arr2.dtype): |
| 327 | # Enforce this for now. The solution to it will be implement add |
| 328 | # as a ufunc. It never worked right on Python 3: bytes + unicode gave |
| 329 | # nonsense unicode + bytes errored, and unicode + object used the |
| 330 | # object dtype itemsize as num chars (worked on short strings). |
| 331 | # bytes + void worked but promoting void->bytes is dubious also. |
| 332 | raise TypeError( |
| 333 | "np.char.add() requires both arrays of the same dtype kind, but " |
| 334 | f"got dtypes: '{arr1.dtype}' and '{arr2.dtype}' (the few cases " |
| 335 | "where this used to work often lead to incorrect results).") |
| 336 | |
| 337 | return _vec_string(arr1, type(arr1.dtype)(out_size), '__add__', (arr2,)) |
| 338 | |
| 339 | def _multiply_dispatcher(a, i): |
| 340 | return (a,) |