@@ -664,6 +664,8 @@ def test_empty_str_methods(self):
664
664
tm .assert_series_equal (empty_str , empty .str .pad (42 ))
665
665
tm .assert_series_equal (empty_str , empty .str .center (42 ))
666
666
tm .assert_series_equal (empty_list , empty .str .split ('a' ))
667
+ tm .assert_series_equal (empty_list , empty .str .partition ('a' , return_type = 'series' ))
668
+ tm .assert_series_equal (empty_list , empty .str .rpartition ('a' , return_type = 'series' ))
667
669
tm .assert_series_equal (empty_str , empty .str .slice (stop = 1 ))
668
670
tm .assert_series_equal (empty_str , empty .str .slice (step = 1 ))
669
671
tm .assert_series_equal (empty_str , empty .str .strip ())
@@ -686,6 +688,13 @@ def test_empty_str_methods(self):
686
688
tm .assert_series_equal (empty_str , empty .str .capitalize ())
687
689
tm .assert_series_equal (empty_str , empty .str .swapcase ())
688
690
691
+ def test_empty_str_methods_to_frame (self ):
692
+ empty_str = empty = Series (dtype = str )
693
+ empty_df = DataFrame ([])
694
+
695
+ tm .assert_frame_equal (empty_df , empty .str .partition ('a' ))
696
+ tm .assert_frame_equal (empty_df , empty .str .rpartition ('a' ))
697
+
689
698
def test_ismethods (self ):
690
699
values = ['A' , 'b' , 'Xy' , '4' , '3A' , '' , 'TT' , '55' , '-' , ' ' ]
691
700
str_s = Series (values )
@@ -1174,6 +1183,97 @@ def test_split_to_dataframe(self):
1174
1183
with tm .assertRaisesRegexp (ValueError , "return_type must be" ):
1175
1184
s .str .split ('_' , return_type = "some_invalid_type" )
1176
1185
1186
+ def test_partition_series (self ):
1187
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
1188
+
1189
+ result = values .str .partition ('_' , return_type = 'series' )
1190
+ exp = Series ([['a' , '_' , 'b_c' ], ['c' , '_' , 'd_e' ], NA , ['f' , '_' , 'g_h' ]])
1191
+ tm .assert_series_equal (result , exp )
1192
+
1193
+ result = values .str .rpartition ('_' , return_type = 'series' )
1194
+ exp = Series ([['a_b' , '_' , 'c' ], ['c_d' , '_' , 'e' ], NA , ['f_g' , '_' , 'h' ]])
1195
+ tm .assert_series_equal (result , exp )
1196
+
1197
+ # more than one char
1198
+ values = Series (['a__b__c' , 'c__d__e' , NA , 'f__g__h' ])
1199
+ result = values .str .partition ('__' , return_type = 'series' )
1200
+ exp = Series ([['a' , '__' , 'b__c' ], ['c' , '__' , 'd__e' ], NA , ['f' , '__' , 'g__h' ]])
1201
+ tm .assert_series_equal (result , exp )
1202
+
1203
+ result = values .str .rpartition ('__' , return_type = 'series' )
1204
+ exp = Series ([['a__b' , '__' , 'c' ], ['c__d' , '__' , 'e' ], NA , ['f__g' , '__' , 'h' ]])
1205
+ tm .assert_series_equal (result , exp )
1206
+
1207
+ # None
1208
+ values = Series (['a b c' , 'c d e' , NA , 'f g h' ])
1209
+ result = values .str .partition (return_type = 'series' )
1210
+ exp = Series ([['a' , ' ' , 'b c' ], ['c' , ' ' , 'd e' ], NA , ['f' , ' ' , 'g h' ]])
1211
+ tm .assert_series_equal (result , exp )
1212
+
1213
+ result = values .str .rpartition (return_type = 'series' )
1214
+ exp = Series ([['a b' , ' ' , 'c' ], ['c d' , ' ' , 'e' ], NA , ['f g' , ' ' , 'h' ]])
1215
+ tm .assert_series_equal (result , exp )
1216
+
1217
+ # Not splited
1218
+ values = Series (['abc' , 'cde' , NA , 'fgh' ])
1219
+ result = values .str .partition ('_' , return_type = 'series' )
1220
+ exp = Series ([['abc' , '' , '' ], ['cde' , '' , '' ], NA , ['fgh' , '' , '' ]])
1221
+ tm .assert_series_equal (result , exp )
1222
+
1223
+ result = values .str .rpartition ('_' , return_type = 'series' )
1224
+ exp = Series ([['' , '' , 'abc' ], ['' , '' , 'cde' ], NA , ['' , '' , 'fgh' ]])
1225
+ tm .assert_series_equal (result , exp )
1226
+
1227
+ # unicode
1228
+ values = Series ([u ('a_b_c' ), u ('c_d_e' ), NA , u ('f_g_h' )])
1229
+
1230
+ result = values .str .partition ('_' , return_type = 'series' )
1231
+ exp = Series ([[u ('a' ), u ('_' ), u ('b_c' )], [u ('c' ), u ('_' ), u ('d_e' )],
1232
+ NA , [u ('f' ), u ('_' ), u ('g_h' )]])
1233
+ tm .assert_series_equal (result , exp )
1234
+
1235
+ result = values .str .rpartition ('_' , return_type = 'series' )
1236
+ exp = Series ([[u ('a_b' ), u ('_' ), u ('c' )], [u ('c_d' ), u ('_' ), u ('e' )],
1237
+ NA , [u ('f_g' ), u ('_' ), u ('h' )]])
1238
+ tm .assert_series_equal (result , exp )
1239
+
1240
+ # compare to standard lib
1241
+ values = Series (['A_B_C' , 'B_C_D' , 'E_F_G' , 'EFGHEF' ])
1242
+ result = values .str .partition ('_' , return_type = 'series' ).tolist ()
1243
+ self .assertEqual (result , [v .partition ('_' ) for v in values ])
1244
+ result = values .str .rpartition ('_' , return_type = 'series' ).tolist ()
1245
+ self .assertEqual (result , [v .rpartition ('_' ) for v in values ])
1246
+
1247
+ def test_partition_to_dataframe (self ):
1248
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
1249
+ result = values .str .partition ('_' )
1250
+ exp = DataFrame ({0 : ['a' , 'c' , np .nan , 'f' ],
1251
+ 1 : ['_' , '_' , np .nan , '_' ],
1252
+ 2 : ['b_c' , 'd_e' , np .nan , 'g_h' ]})
1253
+ tm .assert_frame_equal (result , exp )
1254
+
1255
+ result = values .str .rpartition ('_' )
1256
+ exp = DataFrame ({0 : ['a_b' , 'c_d' , np .nan , 'f_g' ],
1257
+ 1 : ['_' , '_' , np .nan , '_' ],
1258
+ 2 : ['c' , 'e' , np .nan , 'h' ]})
1259
+ tm .assert_frame_equal (result , exp )
1260
+
1261
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
1262
+ result = values .str .partition ('_' , return_type = 'frame' )
1263
+ exp = DataFrame ({0 : ['a' , 'c' , np .nan , 'f' ],
1264
+ 1 : ['_' , '_' , np .nan , '_' ],
1265
+ 2 : ['b_c' , 'd_e' , np .nan , 'g_h' ]})
1266
+ tm .assert_frame_equal (result , exp )
1267
+
1268
+ result = values .str .rpartition ('_' , return_type = 'frame' )
1269
+ exp = DataFrame ({0 : ['a_b' , 'c_d' , np .nan , 'f_g' ],
1270
+ 1 : ['_' , '_' , np .nan , '_' ],
1271
+ 2 : ['c' , 'e' , np .nan , 'h' ]})
1272
+ tm .assert_frame_equal (result , exp )
1273
+
1274
+ with tm .assertRaisesRegexp (ValueError , "return_type must be" ):
1275
+ values .str .partition ('_' , return_type = "some_invalid_type" )
1276
+
1177
1277
def test_pipe_failures (self ):
1178
1278
# #2119
1179
1279
s = Series (['A|B|C' ])
0 commit comments