Skip to content

Commit e9c45ba

Browse files
committed
ENH: support str translate for StringMethods
1 parent 5e994b6 commit e9c45ba

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ strings and apply several methods to it. These can be acccessed like
552552
Series.str.strip
553553
Series.str.swapcase
554554
Series.str.title
555+
Series.str.translate
555556
Series.str.upper
556557
Series.str.wrap
557558
Series.str.zfill

doc/source/text.rst

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ Method Summary
268268
:meth:`~Series.str.rfind`,Equivalent to ``str.rfind``
269269
:meth:`~Series.str.capitalize`,Equivalent to ``str.capitalize``
270270
:meth:`~Series.str.swapcase`,Equivalent to ``str.swapcase``
271+
:meth:`~Series.str.translate`,Equivalent to ``str.translate``
271272
:meth:`~Series.str.isalnum`,Equivalent to ``str.isalnum``
272273
:meth:`~Series.str.isalpha`,Equivalent to ``str.isalpha``
273274
:meth:`~Series.str.isdigit`,Equivalent to ``str.isdigit``

pandas/core/strings.py

+78
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,65 @@ def str_wrap(arr, width, **kwargs):
834834
return _na_map(lambda s: '\n'.join(tw.wrap(s)), arr)
835835

836836

837+
def str_translate_py3(arr, table):
838+
"""
839+
Map all characters in the string through the given translation table.
840+
Equivalent to standard ``str.translate``.
841+
842+
Parameters
843+
----------
844+
table : dict
845+
A mapping of Unicode ordinals to Unicode ordinals, strings, or None.
846+
nmapped characters are left untouched. Characters mapped to None
847+
are deleted.
848+
849+
Returns
850+
-------
851+
translated : array
852+
853+
Notes
854+
-----
855+
``str.maketrans`` is a helper function for making translation tables
856+
"""
857+
f = lambda x: x.translate(table)
858+
return _na_map(f, arr)
859+
860+
861+
def str_translate_py2(arr, table, deletechars=None):
862+
"""
863+
Map all characters in the string through the given translation table and
864+
optional argument deletechars. Equivalent to standard ``str.translate``.
865+
866+
Translate each string such that all characters occurring in the optional
867+
argument deletechars are removed, and the remaining characters are mapped
868+
through the given translation table, which must be a string of length 256
869+
or None. If the table argument is None, no translation is applied and
870+
the operation simply removes the characters in deletechars.
871+
872+
Parameters
873+
----------
874+
table : str or None
875+
Either a string of length 256 or None.
876+
If the table argument is None, no translation is applied and
877+
the operation simply removes the characters in deletechars.
878+
deletechars : str, optional
879+
A string of characters to delete.
880+
881+
Returns
882+
-------
883+
translated : array
884+
885+
Notes
886+
-----
887+
``string.maketrans`` is a helper function for making translation tables
888+
"""
889+
if deletechars is None:
890+
f = lambda x: x.translate(table)
891+
else:
892+
f = lambda x: x.translate(table, deletechars)
893+
return _na_map(f, arr)
894+
895+
837896
def str_get(arr, i):
838897
"""
839898
Extract element from lists, tuples, or strings in each element in the array
@@ -921,6 +980,24 @@ def wrapper3(self, pat, na=np.nan):
921980
return wrapper
922981

923982

983+
def _str_translate_wrapper():
984+
from pandas import compat
985+
# python2 has a different signature for str.translate from python3
986+
if not compat.PY3:
987+
f = str_translate_py2
988+
def wrapper(self, table, deletechars=None):
989+
result = f(self.series, table, deletechars)
990+
return self._wrap_result(result)
991+
else:
992+
f = str_translate_py3
993+
def wrapper(self, table):
994+
result = f(self.series, table)
995+
return self._wrap_result(result)
996+
if f.__doc__:
997+
wrapper.__doc__ = f.__doc__
998+
return wrapper
999+
1000+
9241001
def copy(source):
9251002
"Copy a docstring from another source function (if present)"
9261003
def do_copy(target):
@@ -1125,6 +1202,7 @@ def get_dummies(self, sep='|'):
11251202
endswith = _pat_wrapper(str_endswith, na=True)
11261203
findall = _pat_wrapper(str_findall, flags=True)
11271204
extract = _pat_wrapper(str_extract, flags=True)
1205+
translate = _str_translate_wrapper()
11281206

11291207
_shared_docs['find'] = ("""
11301208
Return %(side)s indexes in each strings where the substring is

pandas/tests/test_strings.py

+22
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,28 @@ def test_pad_fillchar(self):
965965
with tm.assertRaisesRegexp(TypeError, "fillchar must be a character, not int"):
966966
result = values.str.pad(5, fillchar=5)
967967

968+
def test_translate(self):
969+
for klass in [Series, Index]:
970+
s = klass(['abcdefg', 'abcc', 'cdddfg', 'cdefggg'])
971+
if not compat.PY3:
972+
import string
973+
table = string.maketrans('abc', 'cde')
974+
else:
975+
table = str.maketrans('abc', 'cde')
976+
result = s.str.translate(table)
977+
expected = klass(['cdedefg', 'cdee', 'edddfg', 'edefggg'])
978+
tm.assert_array_equal(result, expected)
979+
980+
# python2 only
981+
if not compat.PY3:
982+
result = s.str.translate(table, 'fg')
983+
expected = klass(['cdede', 'cdee', 'eddd', 'ede'])
984+
tm.assert_array_equal(result, expected)
985+
986+
result = s.str.translate(None, 'fg')
987+
expected = klass(['abcde', 'abcc', 'cddd', 'cde'])
988+
tm.assert_array_equal(result, expected)
989+
968990
def test_center_ljust_rjust(self):
969991
values = Series(['a', 'b', NA, 'c', NA, 'eeeeee'])
970992

0 commit comments

Comments
 (0)