Skip to content

BUG: string methods with NA #31684

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
merged 4 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -29,6 +29,10 @@ Bug fixes

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

**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 isinstance(x, libmissing.NAType):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the same change for scalar_rep? (and a test that hits it)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If sequence passed to repeat has None it is handled...have added a test for it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of isinstance, just use if x is NA

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. Have updated.

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

# GH: 31632
values = Series(["a", None])
result = values.str.repeat([3, 4])
exp = Series(["aaa", None])
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