Skip to content

BUG: Align Series.str.zfill() with str.zfill() #21874

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 10 additions & 4 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2557,20 +2557,26 @@ def zfill(self, width):

Note that ``10`` and ``NaN`` are not strings, therefore they are
converted to ``NaN``. The minus sign in ``'-1'`` is treated as a
regular character and the zero is added to the left of it
special character and the zero is added to the right of it
(:meth:`str.zfill` would have moved it to the left). ``1000``
remains unchanged as it is longer than `width`.

>>> s.str.zfill(3)
0 0-1
0 -01
1 001
2 1000
3 NaN
4 NaN
dtype: object
"""
result = str_pad(self._data, width, side='left', fillchar='0')
return self._wrap_result(result)

if not is_integer(width):
msg = 'width must be of integer type, not {0}'
raise TypeError(msg.format(type(width).__name__))
Copy link
Member

@gfyoung gfyoung Jul 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test for this validation.


f = lambda x: x.zfill(width)

return self._wrap_result(_na_map(f, self._data))

@copy(str_slice)
def slice(self, start=None, stop=None, step=None):
Expand Down
11 changes: 6 additions & 5 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2220,25 +2220,26 @@ def test_center_ljust_rjust_fillchar(self):
result = values.str.rjust(5, fillchar=1)

def test_zfill(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue mentions also affecting rjust, pad, center, can you add / update tests as needed for those as well.

values = Series(['1', '22', 'aaa', '333', '45678'])
values = Series(['1', '+1', '-1', '22', 'aaa', '333', '45678'])

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

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

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

def test_split(self):
Expand Down