Skip to content

Commit 7d5e4f8

Browse files
committed
BUG: Series.str.repeat can handle pd.NA for vectored inputs (#31632)
1 parent 0bfb8cb commit 7d5e4f8

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/source/whatsnew/v1.0.2.rst

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Bug fixes
2929

3030
- Using ``pd.NA`` with :meth:`DataFrame.to_json` now correctly outputs a null value instead of an empty object (:issue:`31615`)
3131

32+
**Strings**
33+
34+
- Using ``pd.NA`` with :meth:`Series.str.repeat` now correctly outputs a null value instead of raising error for vector inputs (:issue:`31632`)
35+
3236
.. ---------------------------------------------------------------------------
3337
3438
.. _whatsnew_102.contributors:

pandas/core/strings.py

+2
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,8 @@ def scalar_rep(x):
778778
else:
779779

780780
def rep(x, r):
781+
if isinstance(x, libmissing.NAType):
782+
return x
781783
try:
782784
return bytes.__mul__(x, r)
783785
except TypeError:

pandas/tests/test_strings.py

+6
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,12 @@ def test_repeat(self):
11531153
assert isinstance(rs, Series)
11541154
tm.assert_series_equal(rs, xp)
11551155

1156+
# GH: 31632
1157+
values = Series(["a", None])
1158+
result = values.str.repeat([3, 4])
1159+
exp = Series(["aaa", None])
1160+
tm.assert_series_equal(result, exp)
1161+
11561162
def test_match(self):
11571163
# New match behavior introduced in 0.13
11581164
values = Series(["fooBAD__barBAD", np.nan, "foo"])

0 commit comments

Comments
 (0)