Skip to content

Commit 52a067c

Browse files
committed
ENH: support str translate for StringMethods
1 parent 6178eaa commit 52a067c

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ strings and apply several methods to it. These can be acccessed like
560560
Series.str.strip
561561
Series.str.swapcase
562562
Series.str.title
563+
Series.str.translate
563564
Series.str.upper
564565
Series.str.wrap
565566
Series.str.zfill

doc/source/text.rst

+1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ Method Summary
273273
:meth:`~Series.str.capitalize`,Equivalent to ``str.capitalize``
274274
:meth:`~Series.str.swapcase`,Equivalent to ``str.swapcase``
275275
:meth:`~Series.str.normalize`,Return Unicode normal form. Equivalent to ``unicodedata.normalize``
276+
:meth:`~Series.str.translate`,Equivalent to ``str.translate``
276277
:meth:`~Series.str.isalnum`,Equivalent to ``str.isalnum``
277278
:meth:`~Series.str.isalpha`,Equivalent to ``str.isalpha``
278279
:meth:`~Series.str.isdigit`,Equivalent to ``str.isdigit``

doc/source/whatsnew/v0.16.1.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ String Methods Enhancements
189189
:ref:`Continuing from v0.16.0 <whatsnew_0160.enhancements.string>`, following
190190
enhancements are performed to make string operation easier.
191191

192-
- Following new methods are accesible via ``.str`` accessor to apply the function to each values. This is intended to make it more consistent with standard methods on strings. (:issue:`9766`, :issue:`9773`, :issue:`10031`, :issue:`10045`)
192+
- Following new methods are accesible via ``.str`` accessor to apply the function to each values. This is intended to make it more consistent with standard methods on strings. (:issue:`9766`, :issue:`9773`, :issue:`10031`, :issue:`10045`, :issue:`10052`)
193193

194194
================ =============== =============== =============== ================
195195
.. .. Methods .. ..
196196
================ =============== =============== =============== ================
197197
``capitalize()`` ``swapcase()`` ``normalize()`` ``partition()`` ``rpartition()``
198-
``index()`` ``rindex()``
198+
``index()`` ``rindex()`` ``translate()``
199199
================ =============== =============== =============== ================
200200

201201

pandas/core/strings.py

+43
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,44 @@ def str_wrap(arr, width, **kwargs):
890890
return _na_map(lambda s: '\n'.join(tw.wrap(s)), arr)
891891

892892

893+
def str_translate(arr, table, deletechars=None):
894+
"""
895+
Map all characters in the string through the given mapping table.
896+
Equivalent to standard :meth:`str.translate`. Note that the optional
897+
argument deletechars is only valid if you are using python 2. For python 3,
898+
character deletion should be specified via the table argument.
899+
900+
Parameters
901+
----------
902+
table : dict (python 3), str or None (python 2)
903+
In python 3, table is a mapping of Unicode ordinals to Unicode ordinals,
904+
strings, or None. Unmapped characters are left untouched. Characters
905+
mapped to None are deleted. :meth:`str.maketrans` is a helper function
906+
for making translation tables.
907+
In python 2, table is either a string of length 256 or None. If the
908+
table argument is None, no translation is applied and the operation
909+
simply removes the characters in deletechars. :func:`string.maketrans`
910+
is a helper function for making translation tables.
911+
deletechars : str, optional (python 2)
912+
A string of characters to delete. This argument is only valid
913+
in python 2.
914+
915+
Returns
916+
-------
917+
translated : Series/Index of objects
918+
"""
919+
if deletechars is None:
920+
f = lambda x: x.translate(table)
921+
else:
922+
from pandas import compat
923+
if compat.PY3:
924+
raise ValueError("deletechars is not a valid argument for "
925+
"str.translate in python 3. You should simply "
926+
"specify character deletions in the table argument")
927+
f = lambda x: x.translate(table, deletechars)
928+
return _na_map(f, arr)
929+
930+
893931
def str_get(arr, i):
894932
"""
895933
Extract element from lists, tuples, or strings in each element in the
@@ -1283,6 +1321,11 @@ def get_dummies(self, sep='|'):
12831321
result = str_get_dummies(self.series, sep)
12841322
return self._wrap_result(result)
12851323

1324+
@copy(str_translate)
1325+
def translate(self, table, deletechars=None):
1326+
result = str_translate(self.series, table, deletechars)
1327+
return self._wrap_result(result)
1328+
12861329
count = _pat_wrapper(str_count, flags=True)
12871330
startswith = _pat_wrapper(str_startswith, na=True)
12881331
endswith = _pat_wrapper(str_endswith, na=True)

pandas/tests/test_strings.py

+37
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,12 @@ def test_empty_str_methods(self):
700700
tm.assert_series_equal(empty_str, empty.str.capitalize())
701701
tm.assert_series_equal(empty_str, empty.str.swapcase())
702702
tm.assert_series_equal(empty_str, empty.str.normalize('NFC'))
703+
if compat.PY3:
704+
table = str.maketrans('a', 'b')
705+
else:
706+
import string
707+
table = string.maketrans('a', 'b')
708+
tm.assert_series_equal(empty_str, empty.str.translate(table))
703709

704710
def test_empty_str_methods_to_frame(self):
705711
empty_str = empty = Series(dtype=str)
@@ -1039,6 +1045,37 @@ def test_pad_fillchar(self):
10391045
with tm.assertRaisesRegexp(TypeError, "fillchar must be a character, not int"):
10401046
result = values.str.pad(5, fillchar=5)
10411047

1048+
def test_translate(self):
1049+
for klass in [Series, Index]:
1050+
s = klass(['abcdefg', 'abcc', 'cdddfg', 'cdefggg'])
1051+
if not compat.PY3:
1052+
import string
1053+
table = string.maketrans('abc', 'cde')
1054+
else:
1055+
table = str.maketrans('abc', 'cde')
1056+
result = s.str.translate(table)
1057+
expected = klass(['cdedefg', 'cdee', 'edddfg', 'edefggg'])
1058+
tm.assert_array_equal(result, expected)
1059+
1060+
# use of deletechars is python 2 only
1061+
if not compat.PY3:
1062+
result = s.str.translate(table, deletechars='fg')
1063+
expected = klass(['cdede', 'cdee', 'eddd', 'ede'])
1064+
tm.assert_array_equal(result, expected)
1065+
1066+
result = s.str.translate(None, deletechars='fg')
1067+
expected = klass(['abcde', 'abcc', 'cddd', 'cde'])
1068+
tm.assert_array_equal(result, expected)
1069+
else:
1070+
with tm.assertRaisesRegexp(ValueError, "deletechars is not a valid argument"):
1071+
result = s.str.translate(table, deletechars='fg')
1072+
1073+
# Series with non-string values
1074+
s = Series(['a', 'b', 'c', 1.2])
1075+
expected = Series(['c', 'd', 'e', np.nan])
1076+
result = s.str.translate(table)
1077+
tm.assert_array_equal(result, expected)
1078+
10421079
def test_center_ljust_rjust(self):
10431080
values = Series(['a', 'b', NA, 'c', NA, 'eeeeee'])
10441081

0 commit comments

Comments
 (0)