You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to pass a function as repl to .str.replace so it was used by re.sub. But it seems that a bug fix for #13438 prevented that to happen. What are your thoughts about this?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-bc58c5c60bd2> in <module>()
----> 1 s.str.replace(r'[aeiou]', lambda m: m.group().upper())
/anaconda3/lib/python3.4/site-packages/pandas/core/strings.py in replace(self, pat, repl, n, case, flags)
1504 def replace(self, pat, repl, n=-1, case=True, flags=0):
1505 result = str_replace(self._data, pat, repl, n=n, case=case,
-> 1506 flags=flags)
1507 return self._wrap_result(result)
1508
/anaconda3/lib/python3.4/site-packages/pandas/core/strings.py in str_replace(arr, pat, repl, n, case, flags)
320# Check whether repl is valid (GH 13438)321ifnot is_string_like(repl):
--> 322 raise TypeError("repl must be a string")
323 use_re =not case orlen(pat) >1or flags
324TypeError: repl must be a string
.str.replace now accepts a callable (function) as replacement string.
It now raises a TypeError when repl is not string like nor a callable.
Docstring updated accordingly.
closespandas-dev#15055
Author: Joost Kranendonk <[email protected]>
Author: Joost Kranendonk <[email protected]>
Closespandas-dev#15056 from hzpc-joostk/pandas-GH15055-patch-1 and squashes the following commits:
826730c [Joost Kranendonk] simplify .str.replace TypeError reraising and test
90779ce [Joost Kranendonk] fix linting issues
c2cc13a [Joost Kranendonk] Update v0.20.0.txt
e15dcdf [Joost Kranendonk] fix bug catch TypeError with wrong number of args
40c0d97 [Joost Kranendonk] improve .str.replace with callable
f15ee2a [Joost Kranendonk] improve test .str.replace with callable
14beb21 [Joost Kranendonk] Add test for .str.replace with regex named groups
27065a2 [Joost Kranendonk] Reraise TypeError only with wrong number of args
ae04a3e [Joost Kranendonk] Add whatsnew for .str.replace with callable repl
067a7a8 [Joost Kranendonk] Fix testing bug for .str.replace
30d4727 [Joost Kranendonk] Bug fix in .str.replace type checking done wrong
4baa0a7 [Joost Kranendonk] add tests for .str.replace with callable repl
91c883d [Joost Kranendonk] Update .str.replace docstring
6ecc43d [Joost Kranendonk] BUG: Fix for .str.replace with repl function
I wanted to pass a function as
repl
to.str.replace
so it was used byre.sub
. But it seems that a bug fix for #13438 prevented that to happen. What are your thoughts about this?Code Sample
Problem description
Expected Output
Output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: