Return a copy of `a` with its elements centered in a string of length `width`. Calls `str.center` element-wise. Parameters ---------- a : array_like of str or unicode width : int The length of the resulting strings fillchar : str or unicode, optional
(a, width, fillchar=' ')
| 464 | |
| 465 | @array_function_dispatch(_center_dispatcher) |
| 466 | def center(a, width, fillchar=' '): |
| 467 | """ |
| 468 | Return a copy of `a` with its elements centered in a string of |
| 469 | length `width`. |
| 470 | |
| 471 | Calls `str.center` element-wise. |
| 472 | |
| 473 | Parameters |
| 474 | ---------- |
| 475 | a : array_like of str or unicode |
| 476 | |
| 477 | width : int |
| 478 | The length of the resulting strings |
| 479 | fillchar : str or unicode, optional |
| 480 | The padding character to use (default is space). |
| 481 | |
| 482 | Returns |
| 483 | ------- |
| 484 | out : ndarray |
| 485 | Output array of str or unicode, depending on input |
| 486 | types |
| 487 | |
| 488 | See Also |
| 489 | -------- |
| 490 | str.center |
| 491 | |
| 492 | Notes |
| 493 | ----- |
| 494 | This function is intended to work with arrays of strings. The |
| 495 | fill character is not applied to numeric types. |
| 496 | |
| 497 | Examples |
| 498 | -------- |
| 499 | >>> c = np.array(['a1b2','1b2a','b2a1','2a1b']); c |
| 500 | array(['a1b2', '1b2a', 'b2a1', '2a1b'], dtype='<U4') |
| 501 | >>> np.char.center(c, width=9) |
| 502 | array([' a1b2 ', ' 1b2a ', ' b2a1 ', ' 2a1b '], dtype='<U9') |
| 503 | >>> np.char.center(c, width=9, fillchar='*') |
| 504 | array(['***a1b2**', '***1b2a**', '***b2a1**', '***2a1b**'], dtype='<U9') |
| 505 | >>> np.char.center(c, width=1) |
| 506 | array(['a', '1', 'b', '2'], dtype='<U1') |
| 507 | |
| 508 | """ |
| 509 | a_arr = numpy.asarray(a) |
| 510 | width_arr = numpy.asarray(width) |
| 511 | size = int(numpy.max(width_arr.flat)) |
| 512 | if numpy.issubdtype(a_arr.dtype, numpy.bytes_): |
| 513 | fillchar = asbytes(fillchar) |
| 514 | return _vec_string( |
| 515 | a_arr, type(a_arr.dtype)(size), 'center', (width_arr, fillchar)) |
| 516 | |
| 517 | |
| 518 | def _count_dispatcher(a, sub, start=None, end=None): |
no test coverage detected