Skip to content

Commit 66d2988

Browse files
committed
ENH: support str translate for StringMethods
1 parent b7c3271 commit 66d2988

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
@@ -553,6 +553,7 @@ strings and apply several methods to it. These can be acccessed like
553553
Series.str.strip
554554
Series.str.swapcase
555555
Series.str.title
556+
Series.str.translate
556557
Series.str.upper
557558
Series.str.wrap
558559
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
@@ -861,6 +861,65 @@ def str_wrap(arr, width, **kwargs):
861861
return _na_map(lambda s: '\n'.join(tw.wrap(s)), arr)
862862

863863

864+
def str_translate_py3(arr, table):
865+
"""
866+
Map all characters in the string through the given translation table.
867+
Equivalent to standard ``str.translate``.
868+
869+
Parameters
870+
----------
871+
table : dict
872+
A mapping of Unicode ordinals to Unicode ordinals, strings, or None.
873+
nmapped characters are left untouched. Characters mapped to None
874+
are deleted.
875+
876+
Returns
877+
-------
878+
translated : Series/Index of objects
879+
880+
Notes
881+
-----
882+
``str.maketrans`` is a helper function for making translation tables
883+
"""
884+
f = lambda x: x.translate(table)
885+
return _na_map(f, arr)
886+
887+
888+
def str_translate_py2(arr, table, deletechars=None):
889+
"""
890+
Map all characters in the string through the given translation table and
891+
optional argument deletechars. Equivalent to standard ``str.translate``.
892+
893+
Translate each string such that all characters occurring in the optional
894+
argument deletechars are removed, and the remaining characters are mapped
895+
through the given translation table, which must be a string of length 256
896+
or None. If the table argument is None, no translation is applied and
897+
the operation simply removes the characters in deletechars.
898+
899+
Parameters
900+
----------
901+
table : str or None
902+
Either a string of length 256 or None.
903+
If the table argument is None, no translation is applied and
904+
the operation simply removes the characters in deletechars.
905+
deletechars : str, optional
906+
A string of characters to delete.
907+
908+
Returns
909+
-------
910+
translated : Series/Index of objects
911+
912+
Notes
913+
-----
914+
``string.maketrans`` is a helper function for making translation tables
915+
"""
916+
if deletechars is None:
917+
f = lambda x: x.translate(table)
918+
else:
919+
f = lambda x: x.translate(table, deletechars)
920+
return _na_map(f, arr)
921+
922+
864923
def str_get(arr, i):
865924
"""
866925
Extract element from lists, tuples, or strings in each element in the
@@ -951,6 +1010,24 @@ def wrapper3(self, pat, na=np.nan):
9511010
return wrapper
9521011

9531012

1013+
def _str_translate_wrapper():
1014+
from pandas import compat
1015+
# python2 has a different signature for str.translate from python3
1016+
if not compat.PY3:
1017+
f = str_translate_py2
1018+
def wrapper(self, table, deletechars=None):
1019+
result = f(self.series, table, deletechars)
1020+
return self._wrap_result(result)
1021+
else:
1022+
f = str_translate_py3
1023+
def wrapper(self, table):
1024+
result = f(self.series, table)
1025+
return self._wrap_result(result)
1026+
if f.__doc__:
1027+
wrapper.__doc__ = f.__doc__
1028+
return wrapper
1029+
1030+
9541031
def copy(source):
9551032
"Copy a docstring from another source function (if present)"
9561033
def do_copy(target):
@@ -1170,6 +1247,7 @@ def get_dummies(self, sep='|'):
11701247
endswith = _pat_wrapper(str_endswith, na=True)
11711248
findall = _pat_wrapper(str_findall, flags=True)
11721249
extract = _pat_wrapper(str_extract, flags=True)
1250+
translate = _str_translate_wrapper()
11731251

11741252
_shared_docs['find'] = ("""
11751253
Return %(side)s indexes in each strings in the Series/Index

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)