Skip to content

ENH: Add StringMethods.zfill #9387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ strings and apply several methods to it. These can be acccessed like
Series.str.strip
Series.str.title
Series.str.upper
Series.str.zfill
Series.str.isalnum
Series.str.isalpha
Series.str.isdigit
Expand Down
1 change: 1 addition & 0 deletions doc/source/text.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ Method Summary
:meth:`~Series.str.center`,Equivalent to ``str.center``
:meth:`~Series.str.ljust`,Equivalent to ``str.ljust``
:meth:`~Series.str.rjust`,Equivalent to ``str.rjust``
:meth:`~Series.str.zfill`,Equivalent to ``str.zfill``
:meth:`~Series.str.wrap`,Split long strings into lines with length less than a given width
:meth:`~Series.str.slice`,Slice each string in the Series
:meth:`~Series.str.slice_replace`,Replace slice in each string with passed value
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Enhancements

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

Performance
~~~~~~~~~~~
Expand Down
21 changes: 19 additions & 2 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ def pad(self, width, side='left', fillchar=' '):
return self._wrap_result(result)

_shared_docs['str_pad'] = ("""
"Center" strings, filling %s side with an additional character
Filling %s side of strings with an additional character

Parameters
----------
Expand All @@ -989,7 +989,7 @@ def pad(self, width, side='left', fillchar=' '):

Returns
-------
centered : array
filled : array
""")

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

def zfill(self, width):
""""
Filling left side with 0

Parameters
----------
width : int
Minimum width of resulting string; additional characters will be filled
with 0

Returns
-------
filled : array
"""
result = str_pad(self.series, width, side='left', fillchar='0')
return self._wrap_result(result)

@copy(str_slice)
def slice(self, start=None, stop=None, step=None):
result = str_slice(self.series, start, stop, step)
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,26 @@ def test_center_ljust_rjust_fillchar(self):
with tm.assertRaisesRegexp(TypeError, "fillchar must be a character, not int"):
result = values.str.rjust(5, fillchar=1)

def test_zfill(self):
values = Series(['1', '22', 'aaa', '333', '45678'])

result = values.str.zfill(5)
expected = Series(['00001', '00022', '00aaa', '00333', '45678'])
tm.assert_series_equal(result, expected)
expected = np.array([v.zfill(5) for v in values.values])
tm.assert_numpy_array_equal(result.values, expected)

result = values.str.zfill(3)
expected = Series(['001', '022', 'aaa', '333', '45678'])
tm.assert_series_equal(result, expected)
expected = np.array([v.zfill(3) for v in values.values])
tm.assert_numpy_array_equal(result.values, expected)

values = Series(['1', np.nan, 'aaa', np.nan, '45678'])
result = values.str.zfill(5)
expected = Series(['00001', np.nan, '00aaa', np.nan, '45678'])
tm.assert_series_equal(result, expected)

def test_split(self):
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])

Expand Down