@@ -834,6 +834,65 @@ def str_wrap(arr, width, **kwargs):
834
834
return _na_map (lambda s : '\n ' .join (tw .wrap (s )), arr )
835
835
836
836
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
+
837
896
def str_get (arr , i ):
838
897
"""
839
898
Extract element from lists, tuples, or strings in each element in the array
@@ -921,6 +980,24 @@ def wrapper3(self, pat, na=np.nan):
921
980
return wrapper
922
981
923
982
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
+
924
1001
def copy (source ):
925
1002
"Copy a docstring from another source function (if present)"
926
1003
def do_copy (target ):
@@ -1125,6 +1202,7 @@ def get_dummies(self, sep='|'):
1125
1202
endswith = _pat_wrapper (str_endswith , na = True )
1126
1203
findall = _pat_wrapper (str_findall , flags = True )
1127
1204
extract = _pat_wrapper (str_extract , flags = True )
1205
+ translate = _str_translate_wrapper ()
1128
1206
1129
1207
_shared_docs ['find' ] = ("""
1130
1208
Return %(side)s indexes in each strings where the substring is
0 commit comments