Skip to content

Commit

Permalink
ENH: Add StringMethods.zfill
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhrks committed Feb 2, 2015
1 parent 9f439f0 commit 62087af
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/api.rst
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
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
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
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
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

0 comments on commit 62087af

Please sign in to comment.