Return the numeric string left-filled with zeros. A leading sign prefix (``+``/``-``) is handled by inserting the padding after the sign character rather than before. Parameters ---------- a : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype width : arra
(a, width)
| 894 | @set_module("numpy.strings") |
| 895 | @array_function_dispatch(_zfill_dispatcher) |
| 896 | def zfill(a, width): |
| 897 | """ |
| 898 | Return the numeric string left-filled with zeros. A leading |
| 899 | sign prefix (``+``/``-``) is handled by inserting the padding |
| 900 | after the sign character rather than before. |
| 901 | |
| 902 | Parameters |
| 903 | ---------- |
| 904 | a : array-like, with ``StringDType``, ``bytes_``, or ``str_`` dtype |
| 905 | |
| 906 | width : array_like, with any integer dtype |
| 907 | Width of string to left-fill elements in `a`. |
| 908 | |
| 909 | Returns |
| 910 | ------- |
| 911 | out : ndarray |
| 912 | Output array of ``StringDType``, ``bytes_`` or ``str_`` dtype, |
| 913 | depending on input type |
| 914 | |
| 915 | See Also |
| 916 | -------- |
| 917 | str.zfill |
| 918 | |
| 919 | Examples |
| 920 | -------- |
| 921 | >>> import numpy as np |
| 922 | >>> np.strings.zfill(['1', '-1', '+1'], 3) |
| 923 | array(['001', '-01', '+01'], dtype='<U3') |
| 924 | |
| 925 | """ |
| 926 | width = np.asanyarray(width) |
| 927 | if not np.issubdtype(width.dtype, np.integer): |
| 928 | raise TypeError(f"unsupported type {width.dtype} for operand 'width'") |
| 929 | |
| 930 | a = np.asanyarray(a) |
| 931 | |
| 932 | if a.dtype.char == "T": |
| 933 | return _zfill(a, width) |
| 934 | |
| 935 | width = np.maximum(str_len(a), width) |
| 936 | shape = np.broadcast_shapes(a.shape, width.shape) |
| 937 | out_dtype = f"{a.dtype.char}{width.max()}" |
| 938 | out = np.empty_like(a, shape=shape, dtype=out_dtype) |
| 939 | return _zfill(a, width, out=out) |
| 940 | |
| 941 | |
| 942 | @set_module("numpy.strings") |