@@ -617,6 +617,8 @@ def test_empty_str_methods(self):
617
617
tm .assert_series_equal (empty_str , empty .str .pad (42 ))
618
618
tm .assert_series_equal (empty_str , empty .str .center (42 ))
619
619
tm .assert_series_equal (empty_list , empty .str .split ('a' ))
620
+ tm .assert_series_equal (empty_list , empty .str .partition ('a' ))
621
+ tm .assert_series_equal (empty_list , empty .str .rpartition ('a' ))
620
622
tm .assert_series_equal (empty_str , empty .str .slice (stop = 1 ))
621
623
tm .assert_series_equal (empty_str , empty .str .slice (step = 1 ))
622
624
tm .assert_series_equal (empty_str , empty .str .strip ())
@@ -1125,6 +1127,82 @@ def test_split_to_dataframe(self):
1125
1127
with tm .assertRaisesRegexp (ValueError , "return_type must be" ):
1126
1128
s .str .split ('_' , return_type = "some_invalid_type" )
1127
1129
1130
+ def test_partition (self ):
1131
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
1132
+
1133
+ result = values .str .partition ('_' )
1134
+ exp = Series ([['a' , '_' , 'b_c' ], ['c' , '_' , 'd_e' ], NA , ['f' , '_' , 'g_h' ]])
1135
+ tm .assert_series_equal (result , exp )
1136
+
1137
+ result = values .str .rpartition ('_' )
1138
+ exp = Series ([['a_b' , '_' , 'c' ], ['c_d' , '_' , 'e' ], NA , ['f_g' , '_' , 'h' ]])
1139
+ tm .assert_series_equal (result , exp )
1140
+
1141
+ # more than one char
1142
+ values = Series (['a__b__c' , 'c__d__e' , NA , 'f__g__h' ])
1143
+ result = values .str .partition ('__' )
1144
+ exp = Series ([['a' , '__' , 'b__c' ], ['c' , '__' , 'd__e' ], NA , ['f' , '__' , 'g__h' ]])
1145
+ tm .assert_series_equal (result , exp )
1146
+
1147
+ result = values .str .rpartition ('__' )
1148
+ exp = Series ([['a__b' , '__' , 'c' ], ['c__d' , '__' , 'e' ], NA , ['f__g' , '__' , 'h' ]])
1149
+ tm .assert_series_equal (result , exp )
1150
+
1151
+ # None
1152
+ values = Series (['a b c' , 'c d e' , NA , 'f g h' ])
1153
+ result = values .str .partition ()
1154
+ exp = Series ([['a' , ' ' , 'b c' ], ['c' , ' ' , 'd e' ], NA , ['f' , ' ' , 'g h' ]])
1155
+ tm .assert_series_equal (result , exp )
1156
+
1157
+ result = values .str .rpartition ()
1158
+ exp = Series ([['a b' , ' ' , 'c' ], ['c d' , ' ' , 'e' ], NA , ['f g' , ' ' , 'h' ]])
1159
+ tm .assert_series_equal (result , exp )
1160
+
1161
+ # Not splited
1162
+ values = Series (['abc' , 'cde' , NA , 'fgh' ])
1163
+ result = values .str .partition ('_' )
1164
+ exp = Series ([['abc' , '' , '' ], ['cde' , '' , '' ], NA , ['fgh' , '' , '' ]])
1165
+ tm .assert_series_equal (result , exp )
1166
+
1167
+ result = values .str .rpartition ('_' )
1168
+ exp = Series ([['' , '' , 'abc' ], ['' , '' , 'cde' ], NA , ['' , '' , 'fgh' ]])
1169
+ tm .assert_series_equal (result , exp )
1170
+
1171
+ # unicode
1172
+ values = Series ([u ('a_b_c' ), u ('c_d_e' ), NA , u ('f_g_h' )])
1173
+
1174
+ result = values .str .partition ('_' )
1175
+ exp = Series ([[u ('a' ), u ('_' ), u ('b_c' )], [u ('c' ), u ('_' ), u ('d_e' )],
1176
+ NA , [u ('f' ), u ('_' ), u ('g_h' )]])
1177
+ tm .assert_series_equal (result , exp )
1178
+
1179
+ result = values .str .rpartition ('_' )
1180
+ exp = Series ([[u ('a_b' ), u ('_' ), u ('c' )], [u ('c_d' ), u ('_' ), u ('e' )],
1181
+ NA , [u ('f_g' ), u ('_' ), u ('h' )]])
1182
+ tm .assert_series_equal (result , exp )
1183
+
1184
+ # compare to standard lib
1185
+ values = Series (['A_B_C' , 'B_C_D' , 'E_F_G' , 'EFGHEF' ])
1186
+ self .assertEqual (values .str .partition ('_' ).tolist (), [v .partition ('_' ) for v in values ])
1187
+ self .assertEqual (values .str .rpartition ('_' ).tolist (), [v .rpartition ('_' ) for v in values ])
1188
+
1189
+ def test_partition_to_dataframe (self ):
1190
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
1191
+ result = values .str .partition ('_' , return_type = 'frame' )
1192
+ exp = DataFrame ({0 : ['a' , 'c' , np .nan , 'f' ],
1193
+ 1 : ['_' , '_' , np .nan , '_' ],
1194
+ 2 : ['b_c' , 'd_e' , np .nan , 'g_h' ]})
1195
+ tm .assert_frame_equal (result , exp )
1196
+
1197
+ result = values .str .rpartition ('_' , return_type = 'frame' )
1198
+ exp = DataFrame ({0 : ['a_b' , 'c_d' , np .nan , 'f_g' ],
1199
+ 1 : ['_' , '_' , np .nan , '_' ],
1200
+ 2 : ['c' , 'e' , np .nan , 'h' ]})
1201
+ tm .assert_frame_equal (result , exp )
1202
+
1203
+ with tm .assertRaisesRegexp (ValueError , "return_type must be" ):
1204
+ values .str .partition ('_' , return_type = "some_invalid_type" )
1205
+
1128
1206
def test_pipe_failures (self ):
1129
1207
# #2119
1130
1208
s = Series (['A|B|C' ])
0 commit comments