@@ -861,6 +861,65 @@ def str_wrap(arr, width, **kwargs):
861
861
return _na_map (lambda s : '\n ' .join (tw .wrap (s )), arr )
862
862
863
863
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
+
864
923
def str_get (arr , i ):
865
924
"""
866
925
Extract element from lists, tuples, or strings in each element in the
@@ -951,6 +1010,24 @@ def wrapper3(self, pat, na=np.nan):
951
1010
return wrapper
952
1011
953
1012
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
+
954
1031
def copy (source ):
955
1032
"Copy a docstring from another source function (if present)"
956
1033
def do_copy (target ):
@@ -1170,6 +1247,7 @@ def get_dummies(self, sep='|'):
1170
1247
endswith = _pat_wrapper (str_endswith , na = True )
1171
1248
findall = _pat_wrapper (str_findall , flags = True )
1172
1249
extract = _pat_wrapper (str_extract , flags = True )
1250
+ translate = _str_translate_wrapper ()
1173
1251
1174
1252
_shared_docs ['find' ] = ("""
1175
1253
Return %(side)s indexes in each strings in the Series/Index
0 commit comments