Skip to content

Commit

Permalink
BUG: Fix for .str.replace with invalid input
Browse files Browse the repository at this point in the history
closes #13438

Author: priyankjain <priyankjjain61@gmail.com>

Closes #13460 from priyankjain/13438 and squashes the following commits:

d5c3f1b [priyankjain] BUG: Fix for .str.replace with invalid input
  • Loading branch information
priyankjain authored and jreback committed Jun 16, 2016
1 parent d814f43 commit fca35fb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ Bug Fixes

- Bug in ``DataFrame.to_csv()`` in which float values were being quoted even though quotations were specified for non-numeric values only (:issue:`12922`, :issue:`13259`)
- Bug in ``MultiIndex`` slicing where extra elements were returned when level is non-unique (:issue:`12896`)

- Bug in ``.str.replace`` does not raise ``TypeError`` for invalid replacement (:issue:`13438`)


- Bug in ``pd.read_csv()`` with ``engine='python'`` in which ``NaN`` values weren't being detected after data was converted to numeric values (:issue:`13314`)
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pandas.core.common import (isnull, notnull, _values_from_object,
is_bool_dtype,
is_list_like, is_categorical_dtype,
is_object_dtype)
is_object_dtype, is_string_like)
from pandas.core.algorithms import take_1d
import pandas.compat as compat
from pandas.core.base import AccessorProperty, NoNewAttributesMixin
Expand Down Expand Up @@ -309,6 +309,10 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0):
-------
replaced : Series/Index of objects
"""

# Check whether repl is valid (GH 13438)
if not is_string_like(repl):
raise TypeError("repl must be a string")
use_re = not case or len(pat) > 1 or flags

if use_re:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ def test_replace(self):
result = values.str.replace("(?<=\w),(?=\w)", ", ", flags=re.UNICODE)
tm.assert_series_equal(result, exp)

# GH 13438
for klass in (Series, Index):
for repl in (None, 3, {'a': 'b'}):
for data in (['a', 'b', None], ['a', 'b', 'c', 'ad']):
values = klass(data)
self.assertRaises(TypeError, values.str.replace, 'a', repl)

def test_repeat(self):
values = Series(['a', 'b', NA, 'c', NA, 'd'])

Expand Down

0 comments on commit fca35fb

Please sign in to comment.