Skip to content

Commit 6bcae88

Browse files
alkamidvictor
authored and
victor
committed
DOC: Improve the docstring of String.str.zfill() (pandas-dev#20864)
1 parent c9cea46 commit 6bcae88

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

pandas/core/strings.py

+50-5
Original file line numberDiff line numberDiff line change
@@ -2473,18 +2473,63 @@ def rjust(self, width, fillchar=' '):
24732473

24742474
def zfill(self, width):
24752475
"""
2476-
Filling left side of strings in the Series/Index with 0.
2477-
Equivalent to :meth:`str.zfill`.
2476+
Pad strings in the Series/Index by prepending '0' characters.
2477+
2478+
Strings in the Series/Index are padded with '0' characters on the
2479+
left of the string to reach a total string length `width`. Strings
2480+
in the Series/Index with length greater or equal to `width` are
2481+
unchanged.
24782482
24792483
Parameters
24802484
----------
24812485
width : int
2482-
Minimum width of resulting string; additional characters will be
2483-
filled with 0
2486+
Minimum length of resulting string; strings with length less
2487+
than `width` be prepended with '0' characters.
24842488
24852489
Returns
24862490
-------
2487-
filled : Series/Index of objects
2491+
Series/Index of objects
2492+
2493+
See Also
2494+
--------
2495+
Series.str.rjust: Fills the left side of strings with an arbitrary
2496+
character.
2497+
Series.str.ljust: Fills the right side of strings with an arbitrary
2498+
character.
2499+
Series.str.pad: Fills the specified sides of strings with an arbitrary
2500+
character.
2501+
Series.str.center: Fills boths sides of strings with an arbitrary
2502+
character.
2503+
2504+
Notes
2505+
-----
2506+
Differs from :meth:`str.zfill` which has special handling
2507+
for '+'/'-' in the string.
2508+
2509+
Examples
2510+
--------
2511+
>>> s = pd.Series(['-1', '1', '1000', 10, np.nan])
2512+
>>> s
2513+
0 -1
2514+
1 1
2515+
2 1000
2516+
3 10
2517+
4 NaN
2518+
dtype: object
2519+
2520+
Note that ``10`` and ``NaN`` are not strings, therefore they are
2521+
converted to ``NaN``. The minus sign in ``'-1'`` is treated as a
2522+
regular character and the zero is added to the left of it
2523+
(:meth:`str.zfill` would have moved it to the left). ``1000``
2524+
remains unchanged as it is longer than `width`.
2525+
2526+
>>> s.str.zfill(3)
2527+
0 0-1
2528+
1 001
2529+
2 1000
2530+
3 NaN
2531+
4 NaN
2532+
dtype: object
24882533
"""
24892534
result = str_pad(self._data, width, side='left', fillchar='0')
24902535
return self._wrap_result(result)

0 commit comments

Comments
 (0)