Skip to content

Commit 06795db

Browse files
committed
BUG: "Added tests for width parameter on center, ljust, rjust, zfill."
1 parent 468df3a commit 06795db

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

doc/source/whatsnew/v0.19.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -614,4 +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`)
617+
- Bug in ``pandas.Series.str.zfill``, when ``width`` was not of integer type no ``TypeError`` was raised, which resulted in a series with all NaN values (:issue:`13598`)

pandas/tests/test_strings.py

+38
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,33 @@ def test_center_ljust_rjust_fillchar(self):
17571757
"fillchar must be a character, not int"):
17581758
result = values.str.rjust(5, fillchar=1)
17591759

1760+
def test_center_ljust_rjust_width(self):
1761+
values = Series(['a', 'bb', 'ccc', NA, 'eeeee'])
1762+
1763+
result = values.str.center(4, fillchar='X')
1764+
expected = Series(['XaXX', 'XbbX', 'cccX', NA, 'eeeee'])
1765+
tm.assert_almost_equal(result, expected)
1766+
1767+
result = values.str.ljust(4, fillchar='X')
1768+
expected = Series(['aXXX', 'bbXX', 'cccX', NA, 'eeeee'])
1769+
tm.assert_almost_equal(result, expected)
1770+
1771+
result = values.str.rjust(4, fillchar='X')
1772+
expected = Series(['XXXa', 'XXbb', 'Xccc', NA, 'eeeee'])
1773+
tm.assert_almost_equal(result, expected)
1774+
1775+
with tm.assertRaisesRegexp(TypeError,
1776+
"width must be of integer type, not*"):
1777+
result = values.str.center('f', fillchar='X')
1778+
1779+
with tm.assertRaisesRegexp(TypeError,
1780+
"width must be of integer type, not*"):
1781+
result = values.str.ljust('f', fillchar='X')
1782+
1783+
with tm.assertRaisesRegexp(TypeError,
1784+
"width must be of integer type, not*"):
1785+
result = values.str.rjust('f', fillchar='X')
1786+
17601787
def test_zfill(self):
17611788
values = Series(['1', '22', 'aaa', '333', '45678'])
17621789

@@ -1779,6 +1806,17 @@ def test_zfill(self):
17791806
expected = Series(['00001', np.nan, '00aaa', np.nan, '45678'])
17801807
tm.assert_series_equal(result, expected)
17811808

1809+
def test_zfill_width(self):
1810+
values = Series(['1', '22', 'ccc', NA, 'eeeee'])
1811+
1812+
result = values.str.zfill(5)
1813+
expected = Series(['00001', '00022', '00ccc', NA, 'eeeee'])
1814+
tm.assert_almost_equal(result, expected)
1815+
1816+
with tm.assertRaisesRegexp(TypeError,
1817+
"width must be of integer type, not*"):
1818+
result = values.str.zfill('f')
1819+
17821820
def test_split(self):
17831821
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])
17841822

0 commit comments

Comments
 (0)