Skip to content

Commit 432a4ee

Browse files
prakhar987meeseeksmachine
authored andcommitted
Backport PR pandas-dev#31684: BUG: string methods with NA
1 parent ac9c57c commit 432a4ee

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

doc/source/whatsnew/v1.0.2.rst

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ Bug fixes
8484
- Fixed bug where :meth:`GroupBy.first` and :meth:`GroupBy.last` would raise a ``TypeError`` when groups contained ``pd.NA`` in a column of object dtype (:issue:`32123`)
8585
- Fix bug in :meth:`Series.convert_dtypes` for series with mix of integers and strings (:issue:`32117`)
8686

87+
**Strings**
88+
89+
- Using ``pd.NA`` with :meth:`Series.str.repeat` now correctly outputs a null value instead of raising error for vector inputs (:issue:`31632`)
90+
8791
.. ---------------------------------------------------------------------------
8892
8993
.. _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 x is libmissing.NA:
782+
return x
781783
try:
782784
return bytes.__mul__(x, r)
783785
except TypeError:

pandas/tests/test_strings.py

+12
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,18 @@ def test_repeat(self):
11571157
assert isinstance(rs, Series)
11581158
tm.assert_series_equal(rs, xp)
11591159

1160+
def test_repeat_with_null(self):
1161+
# GH: 31632
1162+
values = Series(["a", None], dtype="string")
1163+
result = values.str.repeat([3, 4])
1164+
exp = Series(["aaa", None], dtype="string")
1165+
tm.assert_series_equal(result, exp)
1166+
1167+
values = Series(["a", "b"], dtype="string")
1168+
result = values.str.repeat([3, None])
1169+
exp = Series(["aaa", None], dtype="string")
1170+
tm.assert_series_equal(result, exp)
1171+
11601172
def test_match(self):
11611173
# New match behavior introduced in 0.13
11621174
values = Series(["fooBAD__barBAD", np.nan, "foo"])

0 commit comments

Comments
 (0)