Skip to content

Commit 9d1a0d5

Browse files
committed
BUG: Fixing Series.str.zfill() to behave same as str.zfill() from standard library (pandas-dev#20868)
1 parent 9d94a95 commit 9d1a0d5

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

pandas/core/strings.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -2557,20 +2557,26 @@ def zfill(self, width):
25572557
25582558
Note that ``10`` and ``NaN`` are not strings, therefore they are
25592559
converted to ``NaN``. The minus sign in ``'-1'`` is treated as a
2560-
regular character and the zero is added to the left of it
2560+
special character and the zero is added to the right of it
25612561
(:meth:`str.zfill` would have moved it to the left). ``1000``
25622562
remains unchanged as it is longer than `width`.
25632563
25642564
>>> s.str.zfill(3)
2565-
0 0-1
2565+
0 -01
25662566
1 001
25672567
2 1000
25682568
3 NaN
25692569
4 NaN
25702570
dtype: object
25712571
"""
2572-
result = str_pad(self._data, width, side='left', fillchar='0')
2573-
return self._wrap_result(result)
2572+
2573+
if not is_integer(width):
2574+
msg = 'width must be of integer type, not {0}'
2575+
raise TypeError(msg.format(type(width).__name__))
2576+
2577+
f = lambda x: x.zfill(width)
2578+
2579+
return self._wrap_result(_na_map(f, self._data))
25742580

25752581
@copy(str_slice)
25762582
def slice(self, start=None, stop=None, step=None):

pandas/tests/test_strings.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -2220,25 +2220,26 @@ def test_center_ljust_rjust_fillchar(self):
22202220
result = values.str.rjust(5, fillchar=1)
22212221

22222222
def test_zfill(self):
2223-
values = Series(['1', '22', 'aaa', '333', '45678'])
2223+
values = Series(['1', '+1', '-1', '22', 'aaa', '333', '45678'])
22242224

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

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

2239-
values = Series(['1', np.nan, 'aaa', np.nan, '45678'])
2240+
values = Series(['1', np.nan, 'aaa', np.nan, '45678', 10])
22402241
result = values.str.zfill(5)
2241-
expected = Series(['00001', np.nan, '00aaa', np.nan, '45678'])
2242+
expected = Series(['00001', np.nan, '00aaa', np.nan, '45678', np.nan])
22422243
tm.assert_series_equal(result, expected)
22432244

22442245
def test_split(self):

0 commit comments

Comments
 (0)