Skip to content

Backport PR #31684 on branch 1.0.x (BUG: string methods with NA) #32578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v1.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ Bug fixes
- 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`)
- Fix bug in :meth:`Series.convert_dtypes` for series with mix of integers and strings (:issue:`32117`)

**Strings**

- Using ``pd.NA`` with :meth:`Series.str.repeat` now correctly outputs a null value instead of raising error for vector inputs (:issue:`31632`)

.. ---------------------------------------------------------------------------

.. _whatsnew_102.contributors:
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,8 @@ def scalar_rep(x):
else:

def rep(x, r):
if x is libmissing.NA:
return x
try:
return bytes.__mul__(x, r)
except TypeError:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,18 @@ def test_repeat(self):
assert isinstance(rs, Series)
tm.assert_series_equal(rs, xp)

def test_repeat_with_null(self):
# GH: 31632
values = Series(["a", None], dtype="string")
result = values.str.repeat([3, 4])
exp = Series(["aaa", None], dtype="string")
tm.assert_series_equal(result, exp)

values = Series(["a", "b"], dtype="string")
result = values.str.repeat([3, None])
exp = Series(["aaa", None], dtype="string")
tm.assert_series_equal(result, exp)

def test_match(self):
# New match behavior introduced in 0.13
values = Series(["fooBAD__barBAD", np.nan, "foo"])
Expand Down