Skip to content

Commit dc6592e

Browse files
author
Tom Augspurger
committed
Merge pull request #9387 from sinhrks/string_zfill
ENH: Add StringMethods.zfill
2 parents 9929973 + 62087af commit dc6592e

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ strings and apply several methods to it. These can be acccessed like
551551
Series.str.strip
552552
Series.str.title
553553
Series.str.upper
554+
Series.str.zfill
554555
Series.str.isalnum
555556
Series.str.isalpha
556557
Series.str.isdigit

doc/source/text.rst

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Method Summary
215215
:meth:`~Series.str.center`,Equivalent to ``str.center``
216216
:meth:`~Series.str.ljust`,Equivalent to ``str.ljust``
217217
:meth:`~Series.str.rjust`,Equivalent to ``str.rjust``
218+
:meth:`~Series.str.zfill`,Equivalent to ``str.zfill``
218219
:meth:`~Series.str.wrap`,Split long strings into lines with length less than a given width
219220
:meth:`~Series.str.slice`,Slice each string in the Series
220221
:meth:`~Series.str.slice_replace`,Replace slice in each string with passed value

doc/source/whatsnew/v0.16.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Enhancements
131131

132132
- Added ``StringMethods.ljust()`` and ``rjust()`` which behave as the same as standard ``str`` (:issue:`9352`)
133133
- ``StringMethods.pad()`` and ``center()`` now accept `fillchar` option to specify filling character (:issue:`9352`)
134+
- Added ``StringMethods.zfill()`` which behave as the same as standard ``str`` (:issue:`9387`)
134135

135136
Performance
136137
~~~~~~~~~~~

pandas/core/strings.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ def pad(self, width, side='left', fillchar=' '):
977977
return self._wrap_result(result)
978978

979979
_shared_docs['str_pad'] = ("""
980-
"Center" strings, filling %s side with an additional character
980+
Filling %s side of strings with an additional character
981981
982982
Parameters
983983
----------
@@ -989,7 +989,7 @@ def pad(self, width, side='left', fillchar=' '):
989989
990990
Returns
991991
-------
992-
centered : array
992+
filled : array
993993
""")
994994

995995
@Appender(_shared_docs['str_pad'] % 'left and right')
@@ -1004,6 +1004,23 @@ def ljust(self, width, fillchar=' '):
10041004
def rjust(self, width, fillchar=' '):
10051005
return self.pad(width, side='left', fillchar=fillchar)
10061006

1007+
def zfill(self, width):
1008+
""""
1009+
Filling left side with 0
1010+
1011+
Parameters
1012+
----------
1013+
width : int
1014+
Minimum width of resulting string; additional characters will be filled
1015+
with 0
1016+
1017+
Returns
1018+
-------
1019+
filled : array
1020+
"""
1021+
result = str_pad(self.series, width, side='left', fillchar='0')
1022+
return self._wrap_result(result)
1023+
10071024
@copy(str_slice)
10081025
def slice(self, start=None, stop=None, step=None):
10091026
result = str_slice(self.series, start, stop, step)

pandas/tests/test_strings.py

+20
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,26 @@ def test_center_ljust_rjust_fillchar(self):
925925
with tm.assertRaisesRegexp(TypeError, "fillchar must be a character, not int"):
926926
result = values.str.rjust(5, fillchar=1)
927927

928+
def test_zfill(self):
929+
values = Series(['1', '22', 'aaa', '333', '45678'])
930+
931+
result = values.str.zfill(5)
932+
expected = Series(['00001', '00022', '00aaa', '00333', '45678'])
933+
tm.assert_series_equal(result, expected)
934+
expected = np.array([v.zfill(5) for v in values.values])
935+
tm.assert_numpy_array_equal(result.values, expected)
936+
937+
result = values.str.zfill(3)
938+
expected = Series(['001', '022', 'aaa', '333', '45678'])
939+
tm.assert_series_equal(result, expected)
940+
expected = np.array([v.zfill(3) for v in values.values])
941+
tm.assert_numpy_array_equal(result.values, expected)
942+
943+
values = Series(['1', np.nan, 'aaa', np.nan, '45678'])
944+
result = values.str.zfill(5)
945+
expected = Series(['00001', np.nan, '00aaa', np.nan, '45678'])
946+
tm.assert_series_equal(result, expected)
947+
928948
def test_split(self):
929949
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])
930950

0 commit comments

Comments
 (0)