@@ -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' , return_type = 'series' ))
621
+ tm .assert_series_equal (empty_list , empty .str .rpartition ('a' , return_type = 'series' ))
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 ())
@@ -637,6 +639,13 @@ def test_empty_str_methods(self):
637
639
tm .assert_series_equal (empty_str , empty .str .isnumeric ())
638
640
tm .assert_series_equal (empty_str , empty .str .isdecimal ())
639
641
642
+ def test_empty_str_methods_to_frame (self ):
643
+ empty_str = empty = Series (dtype = str )
644
+ empty_df = DataFrame ([])
645
+
646
+ tm .assert_frame_equal (empty_df , empty .str .partition ('a' ))
647
+ tm .assert_frame_equal (empty_df , empty .str .rpartition ('a' ))
648
+
640
649
def test_ismethods (self ):
641
650
values = ['A' , 'b' , 'Xy' , '4' , '3A' , '' , 'TT' , '55' , '-' , ' ' ]
642
651
str_s = Series (values )
@@ -1125,6 +1134,97 @@ def test_split_to_dataframe(self):
1125
1134
with tm .assertRaisesRegexp (ValueError , "return_type must be" ):
1126
1135
s .str .split ('_' , return_type = "some_invalid_type" )
1127
1136
1137
+ def test_partition_series (self ):
1138
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
1139
+
1140
+ result = values .str .partition ('_' , return_type = 'series' )
1141
+ exp = Series ([['a' , '_' , 'b_c' ], ['c' , '_' , 'd_e' ], NA , ['f' , '_' , 'g_h' ]])
1142
+ tm .assert_series_equal (result , exp )
1143
+
1144
+ result = values .str .rpartition ('_' , return_type = 'series' )
1145
+ exp = Series ([['a_b' , '_' , 'c' ], ['c_d' , '_' , 'e' ], NA , ['f_g' , '_' , 'h' ]])
1146
+ tm .assert_series_equal (result , exp )
1147
+
1148
+ # more than one char
1149
+ values = Series (['a__b__c' , 'c__d__e' , NA , 'f__g__h' ])
1150
+ result = values .str .partition ('__' , return_type = 'series' )
1151
+ exp = Series ([['a' , '__' , 'b__c' ], ['c' , '__' , 'd__e' ], NA , ['f' , '__' , 'g__h' ]])
1152
+ tm .assert_series_equal (result , exp )
1153
+
1154
+ result = values .str .rpartition ('__' , return_type = 'series' )
1155
+ exp = Series ([['a__b' , '__' , 'c' ], ['c__d' , '__' , 'e' ], NA , ['f__g' , '__' , 'h' ]])
1156
+ tm .assert_series_equal (result , exp )
1157
+
1158
+ # None
1159
+ values = Series (['a b c' , 'c d e' , NA , 'f g h' ])
1160
+ result = values .str .partition (return_type = 'series' )
1161
+ exp = Series ([['a' , ' ' , 'b c' ], ['c' , ' ' , 'd e' ], NA , ['f' , ' ' , 'g h' ]])
1162
+ tm .assert_series_equal (result , exp )
1163
+
1164
+ result = values .str .rpartition (return_type = 'series' )
1165
+ exp = Series ([['a b' , ' ' , 'c' ], ['c d' , ' ' , 'e' ], NA , ['f g' , ' ' , 'h' ]])
1166
+ tm .assert_series_equal (result , exp )
1167
+
1168
+ # Not splited
1169
+ values = Series (['abc' , 'cde' , NA , 'fgh' ])
1170
+ result = values .str .partition ('_' , return_type = 'series' )
1171
+ exp = Series ([['abc' , '' , '' ], ['cde' , '' , '' ], NA , ['fgh' , '' , '' ]])
1172
+ tm .assert_series_equal (result , exp )
1173
+
1174
+ result = values .str .rpartition ('_' , return_type = 'series' )
1175
+ exp = Series ([['' , '' , 'abc' ], ['' , '' , 'cde' ], NA , ['' , '' , 'fgh' ]])
1176
+ tm .assert_series_equal (result , exp )
1177
+
1178
+ # unicode
1179
+ values = Series ([u ('a_b_c' ), u ('c_d_e' ), NA , u ('f_g_h' )])
1180
+
1181
+ result = values .str .partition ('_' , return_type = 'series' )
1182
+ exp = Series ([[u ('a' ), u ('_' ), u ('b_c' )], [u ('c' ), u ('_' ), u ('d_e' )],
1183
+ NA , [u ('f' ), u ('_' ), u ('g_h' )]])
1184
+ tm .assert_series_equal (result , exp )
1185
+
1186
+ result = values .str .rpartition ('_' , return_type = 'series' )
1187
+ exp = Series ([[u ('a_b' ), u ('_' ), u ('c' )], [u ('c_d' ), u ('_' ), u ('e' )],
1188
+ NA , [u ('f_g' ), u ('_' ), u ('h' )]])
1189
+ tm .assert_series_equal (result , exp )
1190
+
1191
+ # compare to standard lib
1192
+ values = Series (['A_B_C' , 'B_C_D' , 'E_F_G' , 'EFGHEF' ])
1193
+ result = values .str .partition ('_' , return_type = 'series' ).tolist ()
1194
+ self .assertEqual (result , [v .partition ('_' ) for v in values ])
1195
+ result = values .str .rpartition ('_' , return_type = 'series' ).tolist ()
1196
+ self .assertEqual (result , [v .rpartition ('_' ) for v in values ])
1197
+
1198
+ def test_partition_to_dataframe (self ):
1199
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
1200
+ result = values .str .partition ('_' )
1201
+ exp = DataFrame ({0 : ['a' , 'c' , np .nan , 'f' ],
1202
+ 1 : ['_' , '_' , np .nan , '_' ],
1203
+ 2 : ['b_c' , 'd_e' , np .nan , 'g_h' ]})
1204
+ tm .assert_frame_equal (result , exp )
1205
+
1206
+ result = values .str .rpartition ('_' )
1207
+ exp = DataFrame ({0 : ['a_b' , 'c_d' , np .nan , 'f_g' ],
1208
+ 1 : ['_' , '_' , np .nan , '_' ],
1209
+ 2 : ['c' , 'e' , np .nan , 'h' ]})
1210
+ tm .assert_frame_equal (result , exp )
1211
+
1212
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
1213
+ result = values .str .partition ('_' , return_type = 'frame' )
1214
+ exp = DataFrame ({0 : ['a' , 'c' , np .nan , 'f' ],
1215
+ 1 : ['_' , '_' , np .nan , '_' ],
1216
+ 2 : ['b_c' , 'd_e' , np .nan , 'g_h' ]})
1217
+ tm .assert_frame_equal (result , exp )
1218
+
1219
+ result = values .str .rpartition ('_' , return_type = 'frame' )
1220
+ exp = DataFrame ({0 : ['a_b' , 'c_d' , np .nan , 'f_g' ],
1221
+ 1 : ['_' , '_' , np .nan , '_' ],
1222
+ 2 : ['c' , 'e' , np .nan , 'h' ]})
1223
+ tm .assert_frame_equal (result , exp )
1224
+
1225
+ with tm .assertRaisesRegexp (ValueError , "return_type must be" ):
1226
+ values .str .partition ('_' , return_type = "some_invalid_type" )
1227
+
1128
1228
def test_pipe_failures (self ):
1129
1229
# #2119
1130
1230
s = Series (['A|B|C' ])
0 commit comments