Skip to content

Commit 5a52171

Browse files
wcwagnerjreback
authored andcommitted
BUG: Add type check for width parameter in str.pad method GH13598
closes #13598 Author: wcwagner <[email protected]> Closes #13690 from wcwagner/bug/13598 and squashes the following commits: 9669f3f [wcwagner] BUG: "Replaced isinstance with is_integer, and changed test_pad_width to use getattr" 40a3188 [wcwagner] BUG: "Switched to single test method asserting functions that use pad raise correctly." 06795db [wcwagner] BUG: "Added tests for width parameter on center, ljust, rjust, zfill." 468df3a [wcwagner] BUG: Add type check for width parameter in str.pad method GH13598
1 parent 694fe61 commit 5a52171

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

doc/source/whatsnew/v0.19.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ Bug Fixes
583583
- Bug in ``pd.read_csv()`` with ``engine=='c'`` in which null ``quotechar`` was not accepted even though ``quoting`` was specified as ``None`` (:issue:`13411`)
584584
- Bug in ``pd.read_csv()`` with ``engine=='c'`` in which fields were not properly cast to float when quoting was specified as non-numeric (:issue:`13411`)
585585
- Bug in ``pd.pivot_table()`` where ``margins_name`` is ignored when ``aggfunc`` is a list (:issue:`13354`)
586-
586+
- Bug in ``pd.Series.str.zfill``, ``center``, ``ljust``, ``rjust``, and ``pad`` when passing non-integers, did not raise ``TypeError`` (:issue:`13598`)
587587

588588

589589
- Bug in ``Series`` arithmetic raises ``TypeError`` if it contains datetime-like as ``object`` dtype (:issue:`13043`)

pandas/core/strings.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
is_object_dtype,
99
is_string_like,
1010
is_list_like,
11-
is_scalar)
11+
is_scalar,
12+
is_integer)
1213
from pandas.core.common import _values_from_object
1314

1415
from pandas.core.algorithms import take_1d
@@ -914,6 +915,10 @@ def str_pad(arr, width, side='left', fillchar=' '):
914915
if len(fillchar) != 1:
915916
raise TypeError('fillchar must be a character, not str')
916917

918+
if not is_integer(width):
919+
msg = 'width must be of integer type, not {0}'
920+
raise TypeError(msg.format(type(width).__name__))
921+
917922
if side == 'left':
918923
f = lambda x: x.rjust(width, fillchar)
919924
elif side == 'right':

pandas/tests/test_strings.py

+9
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,15 @@ 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+
# GH 13598
1608+
s = Series(['1', '22', 'a', 'bb'])
1609+
1610+
for f in ['center', 'ljust', 'rjust', 'zfill', 'pad']:
1611+
with tm.assertRaisesRegexp(TypeError,
1612+
"width must be of integer type, not*"):
1613+
getattr(s.str, f)('f')
1614+
16061615
def test_translate(self):
16071616

16081617
def _check(result, expected):

0 commit comments

Comments
 (0)