Skip to content

Commit b4e35eb

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

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

doc/source/api.rst

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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``

doc/source/whatsnew/v0.16.1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Enhancements
2525

2626
- Added ``StringMethods.capitalize()`` and ``swapcase`` which behave as the same as standard ``str`` (:issue:`9766`)
2727
- ``DataFrame.diff`` now takes an ``axis`` parameter that determines the direction of differencing (:issue:`9727`)
28+
- Added support for ``StringMethods.translate()`` (:issue:`10052`)
2829
- Added ``StringMethods`` (.str accessor) to ``Index`` (:issue:`9068`)
2930
- Allow clip, clip_lower, and clip_upper to accept array-like arguments as thresholds (:issue:`6966`). These methods now have an ``axis`` parameter which determines how the Series or DataFrame will be aligned with the threshold(s).
3031

pandas/core/strings.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,44 @@ def str_wrap(arr, width, **kwargs):
861861
return _na_map(lambda s: '\n'.join(tw.wrap(s)), arr)
862862

863863

864+
def str_translate(arr, table, deletechars=None):
865+
"""
866+
Map all characters in the string through the given mapping table.
867+
Equivalent to standard ``str.translate``. Note that the optional argument
868+
deletechars is only valid if you are using python 2. For python 3,
869+
character deletion should be specified via the table argument.
870+
871+
Parameters
872+
----------
873+
table : dict (python 3), str or None (python 2)
874+
In python 3, table is a mapping of Unicode ordinals to Unicode ordinals,
875+
strings, or None. Unmapped characters are left untouched. Characters
876+
mapped to None are deleted. ``str.maketrans`` is a helper function for
877+
making translation tables.
878+
In python 2, table is either a string of length 256 or None. If the
879+
table argument is None, no translation is applied and the operation
880+
simply removes the characters in deletechars. ``string.maketrans`` is a
881+
helper function for making translation tables.
882+
deletechars : str, optional (python 2)
883+
A string of characters to delete. This argument is only valid
884+
in python 2.
885+
886+
Returns
887+
-------
888+
translated : Series/Index of objects
889+
"""
890+
if deletechars is None:
891+
f = lambda x: x.translate(table)
892+
else:
893+
from pandas import compat
894+
if compat.PY3:
895+
raise ValueError("deletechars is not a valid argument for "
896+
"str.translate in python 3. You should simply "
897+
"specify character deletions in the table argument")
898+
f = lambda x: x.translate(table, deletechars)
899+
return _na_map(f, arr)
900+
901+
864902
def str_get(arr, i):
865903
"""
866904
Extract element from lists, tuples, or strings in each element in the
@@ -1165,6 +1203,11 @@ def get_dummies(self, sep='|'):
11651203
result = str_get_dummies(self.series, sep)
11661204
return self._wrap_result(result)
11671205

1206+
@copy(str_translate)
1207+
def translate(self, table, deletechars=None):
1208+
result = str_translate(self.series, table, deletechars)
1209+
return self._wrap_result(result)
1210+
11681211
count = _pat_wrapper(str_count, flags=True)
11691212
startswith = _pat_wrapper(str_startswith, na=True)
11701213
endswith = _pat_wrapper(str_endswith, na=True)

pandas/tests/test_strings.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,31 @@ 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+
# use of deletechars is python 2 only
981+
if not compat.PY3:
982+
result = s.str.translate(table, deletechars='fg')
983+
expected = klass(['cdede', 'cdee', 'eddd', 'ede'])
984+
tm.assert_array_equal(result, expected)
985+
986+
result = s.str.translate(None, deletechars='fg')
987+
expected = klass(['abcde', 'abcc', 'cddd', 'cde'])
988+
tm.assert_array_equal(result, expected)
989+
else:
990+
with tm.assertRaisesRegexp(ValueError, "deletechars is not a valid argument"):
991+
result = s.str.translate(table, deletechars='fg')
992+
968993
def test_center_ljust_rjust(self):
969994
values = Series(['a', 'b', NA, 'c', NA, 'eeeeee'])
970995

0 commit comments

Comments
 (0)