Skip to content

Commit 468df3a

Browse files
committed
BUG: Add type check for width parameter in str.pad method GH13598
1 parent 043879f commit 468df3a

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,4 @@ Bug Fixes
614614
- Bug in ``groupby`` with ``as_index=False`` returns all NaN's when grouping on multiple columns including a categorical one (:issue:`13204`)
615615

616616
- Bug where ``pd.read_gbq()`` could throw ``ImportError: No module named discovery`` as a result of a naming conflict with another python package called apiclient (:issue:`13454`)
617+
- Bug in ``pandas.Series.str.zfill``, no TypeErrors raised when ``width`` was not of integer type (:issue:`13598`)

pandas/core/strings.py

+4
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,10 @@ def str_pad(arr, width, side='left', fillchar=' '):
914914
if len(fillchar) != 1:
915915
raise TypeError('fillchar must be a character, not str')
916916

917+
if not isinstance(width, compat.integer_types):
918+
msg = 'width must be of integer type, not {0}'
919+
raise TypeError(msg.format(type(width).__name__))
920+
917921
if side == 'left':
918922
f = lambda x: x.rjust(width, fillchar)
919923
elif side == 'right':

pandas/tests/test_strings.py

+12
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,18 @@ def test_pad_fillchar(self):
16031603
"fillchar must be a character, not int"):
16041604
result = values.str.pad(5, fillchar=5)
16051605

1606+
def test_pad_width(self):
1607+
1608+
values = Series(['1', '22', 'a', 'bb'])
1609+
1610+
result = values.str.pad(5, side='left', fillchar='0')
1611+
expected = Series(['00001', '00022', '0000a', '000bb'])
1612+
tm.assert_almost_equal(result, expected)
1613+
1614+
with tm.assertRaisesRegexp(TypeError,
1615+
"width must be of integer type, not*"):
1616+
result = values.str.pad('f', fillchar='0')
1617+
16061618
def test_translate(self):
16071619

16081620
def _check(result, expected):

0 commit comments

Comments
 (0)