From d5c3f1b077dd28bfe6f59498610603998f3c796d Mon Sep 17 00:00:00 2001 From: priyankjain Date: Thu, 16 Jun 2016 14:30:37 +0530 Subject: [PATCH] BUG: Fix for .str.replace with invalid input .str.replace now raises TypeError when replacement string is not string like --- doc/source/whatsnew/v0.18.2.txt | 1 + pandas/core/strings.py | 4 +++- pandas/tests/test_strings.py | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.18.2.txt b/doc/source/whatsnew/v0.18.2.txt index b3ce9911d3f4d..c68ec8b4eafb8 100644 --- a/doc/source/whatsnew/v0.18.2.txt +++ b/doc/source/whatsnew/v0.18.2.txt @@ -424,3 +424,4 @@ Bug Fixes - Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`) +- Bug in ``.str.replace`` does not raise TypeError for invalid replacement (:issue:`13438`) \ No newline at end of file diff --git a/pandas/core/strings.py b/pandas/core/strings.py index ca8e701d0ce17..8496aaa286ca7 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -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 @@ -309,6 +309,8 @@ def str_replace(arr, pat, repl, n=-1, case=True, flags=0): ------- replaced : Series/Index of objects """ + if not is_string_like(repl): # Check whether repl is valid (GH 13438) + raise TypeError("repl must be a string") use_re = not case or len(pat) > 1 or flags if use_re: diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index 73f9809a7f042..012257bf13015 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -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 pdClass in (Series, Index): + for repl in (None, 3, {'a': 'b'}): + for data in (['a', 'b', None], ['a', 'b', 'c', 'ad']): + values = pdClass(data) + self.assertRaises(TypeError, values.str.replace, 'a', repl) + def test_repeat(self): values = Series(['a', 'b', NA, 'c', NA, 'd'])