@@ -686,6 +686,12 @@ def test_empty_str_methods(self):
686
686
tm .assert_series_equal (empty_str , empty .str .capitalize ())
687
687
tm .assert_series_equal (empty_str , empty .str .swapcase ())
688
688
tm .assert_series_equal (empty_str , empty .str .normalize ('NFC' ))
689
+ if compat .PY3 :
690
+ table = str .maketrans ('a' , 'b' )
691
+ else :
692
+ import string
693
+ table = string .maketrans ('a' , 'b' )
694
+ tm .assert_series_equal (empty_str , empty .str .translate (table ))
689
695
690
696
def test_ismethods (self ):
691
697
values = ['A' , 'b' , 'Xy' , '4' , '3A' , '' , 'TT' , '55' , '-' , ' ' ]
@@ -966,6 +972,37 @@ def test_pad_fillchar(self):
966
972
with tm .assertRaisesRegexp (TypeError , "fillchar must be a character, not int" ):
967
973
result = values .str .pad (5 , fillchar = 5 )
968
974
975
+ def test_translate (self ):
976
+ for klass in [Series , Index ]:
977
+ s = klass (['abcdefg' , 'abcc' , 'cdddfg' , 'cdefggg' ])
978
+ if not compat .PY3 :
979
+ import string
980
+ table = string .maketrans ('abc' , 'cde' )
981
+ else :
982
+ table = str .maketrans ('abc' , 'cde' )
983
+ result = s .str .translate (table )
984
+ expected = klass (['cdedefg' , 'cdee' , 'edddfg' , 'edefggg' ])
985
+ tm .assert_array_equal (result , expected )
986
+
987
+ # use of deletechars is python 2 only
988
+ if not compat .PY3 :
989
+ result = s .str .translate (table , deletechars = 'fg' )
990
+ expected = klass (['cdede' , 'cdee' , 'eddd' , 'ede' ])
991
+ tm .assert_array_equal (result , expected )
992
+
993
+ result = s .str .translate (None , deletechars = 'fg' )
994
+ expected = klass (['abcde' , 'abcc' , 'cddd' , 'cde' ])
995
+ tm .assert_array_equal (result , expected )
996
+ else :
997
+ with tm .assertRaisesRegexp (ValueError , "deletechars is not a valid argument" ):
998
+ result = s .str .translate (table , deletechars = 'fg' )
999
+
1000
+ # Series with non-string values
1001
+ s = Series (['a' , 'b' , 'c' , 1.2 ])
1002
+ expected = Series (['c' , 'd' , 'e' , np .nan ])
1003
+ result = s .str .translate (table )
1004
+ tm .assert_array_equal (result , expected )
1005
+
969
1006
def test_center_ljust_rjust (self ):
970
1007
values = Series (['a' , 'b' , NA , 'c' , NA , 'eeeeee' ])
971
1008
0 commit comments