19
19
import pandas .core .strings as strings
20
20
21
21
22
- def assert_series_or_index_equal (left , right ):
22
+ def assert_series_or_index_equal (left , right , expect_warn = False ):
23
23
if isinstance (left , Series ):
24
24
assert_series_equal (left , right )
25
- else :
25
+ else : # Index
26
26
assert_index_equal (left , right )
27
27
28
28
@@ -158,20 +158,32 @@ def test_str_cat(self, series_or_index):
158
158
exp = Index (['aa' , 'a-' , 'bb' , 'bd' , 'cfoo' , '--' ])
159
159
if series_or_index == 'series' :
160
160
exp = Series (exp )
161
+ # s.index / s is different from t (as Index) -> warning
161
162
with tm .assert_produces_warning (expected_warning = FutureWarning ):
162
163
# FutureWarning to switch to alignment by default
163
164
assert_series_or_index_equal (s .str .cat (t , na_rep = '-' ), exp )
164
165
165
166
# Series/Index with Series
166
167
t = Series (t )
168
+ # s as Series has same index as t -> no warning
169
+ # s as Index is different from t.index -> warning
170
+ if series_or_index == 'series' :
171
+ assert_series_equal (s .str .cat (t , na_rep = '-' ), exp )
172
+ else :
173
+ with tm .assert_produces_warning (expected_warning = FutureWarning ):
174
+ # FutureWarning to switch to alignment by default
175
+ assert_series_or_index_equal (s .str .cat (t , na_rep = '-' ), exp )
176
+
177
+ # Series/Index with Series: warning if different indexes
178
+ t .index = t .index + 1
167
179
with tm .assert_produces_warning (expected_warning = FutureWarning ):
168
180
# FutureWarning to switch to alignment by default
169
181
assert_series_or_index_equal (s .str .cat (t , na_rep = '-' ), exp )
170
182
171
- # Series/Index with array (no warning necessary)
183
+ # Series/Index with array
172
184
assert_series_or_index_equal (s .str .cat (t .values , na_rep = '-' ), exp )
173
185
174
- # Series/Index with list (no warning necessary)
186
+ # Series/Index with list
175
187
assert_series_or_index_equal (s .str .cat (list (t ), na_rep = '-' ), exp )
176
188
177
189
# errors for incorrect lengths
@@ -219,15 +231,29 @@ def test_str_cat_categorical(self, series_or_index,
219
231
exp = Index (['ab' , 'aa' , 'bb' , 'ac' ], dtype = dtype_caller )
220
232
if series_or_index == 'series' :
221
233
exp = Series (exp )
234
+
222
235
# Series/Index with Index
236
+ # s.index / s is different from t (as Index) -> warning
223
237
with tm .assert_produces_warning (expected_warning = FutureWarning ):
224
238
# FutureWarning to switch to alignment by default
225
239
assert_series_or_index_equal (s .str .cat (t ), exp )
226
240
227
241
# Series/Index with Series
242
+ t = Series (t )
243
+ # s as Series has same index as t -> no warning
244
+ # s as Index is different from t.index -> warning
245
+ if series_or_index == 'series' :
246
+ assert_series_equal (s .str .cat (t ), exp )
247
+ else :
248
+ with tm .assert_produces_warning (expected_warning = FutureWarning ):
249
+ # FutureWarning to switch to alignment by default
250
+ assert_series_or_index_equal (s .str .cat (t ), exp )
251
+
252
+ # Series/Index with Series: warning if different indexes
253
+ t .index = t .index + 1
228
254
with tm .assert_produces_warning (expected_warning = FutureWarning ):
229
255
# FutureWarning to switch to alignment by default
230
- assert_series_or_index_equal (s .str .cat (Series ( t ) ), exp )
256
+ assert_series_or_index_equal (s .str .cat (t , na_rep = '-' ), exp )
231
257
232
258
@pytest .mark .parametrize ('series_or_index' , ['series' , 'index' ])
233
259
def test_str_cat_mixed_inputs (self , series_or_index ):
@@ -240,28 +266,62 @@ def test_str_cat_mixed_inputs(self, series_or_index):
240
266
exp = Index (['aAa' , 'bBb' , 'cCc' , 'dDd' ])
241
267
if series_or_index == 'series' :
242
268
exp = Series (exp )
269
+
243
270
# Series/Index with DataFrame
271
+ # s as Series has same index as d -> no warning
272
+ # s as Index is different from d.index -> warning
273
+ if series_or_index == 'series' :
274
+ assert_series_equal (s .str .cat (d ), exp )
275
+ else :
276
+ with tm .assert_produces_warning (expected_warning = FutureWarning ):
277
+ # FutureWarning to switch to alignment by default
278
+ assert_series_or_index_equal (s .str .cat (d ), exp )
279
+
280
+ # Series/Index with DataFrame: warning if different indexes
281
+ d .index = d .index + 1
244
282
with tm .assert_produces_warning (expected_warning = FutureWarning ):
245
283
# FutureWarning to switch to alignment by default
246
284
assert_series_or_index_equal (s .str .cat (d ), exp )
247
285
248
- # Series/Index with two-dimensional ndarray (no warning necessary)
286
+ # Series/Index with two-dimensional ndarray
249
287
assert_series_or_index_equal (s .str .cat (d .values ), exp )
250
288
251
289
# Series/Index with list of Series
290
+ # s as Series has same index as t, s -> no warning
291
+ # s as Index is different from t.index -> warning
292
+ if series_or_index == 'series' :
293
+ assert_series_equal (s .str .cat ([t , s ]), exp )
294
+ else :
295
+ with tm .assert_produces_warning (expected_warning = FutureWarning ):
296
+ # FutureWarning to switch to alignment by default
297
+ assert_series_or_index_equal (s .str .cat ([t , s ]), exp )
298
+
299
+ # Series/Index with list of Series: warning if different indexes
300
+ tt = t .copy ()
301
+ tt .index = tt .index + 1
252
302
with tm .assert_produces_warning (expected_warning = FutureWarning ):
253
303
# FutureWarning to switch to alignment by default
254
- assert_series_or_index_equal (s .str .cat ([t , s ]), exp )
304
+ assert_series_or_index_equal (s .str .cat ([tt , s ]), exp )
255
305
256
- # Series/Index with list of list-likes (no warning necessary)
306
+ # Series/Index with list of list-likes
257
307
assert_series_or_index_equal (s .str .cat ([t .values , list (s )]), exp )
258
308
259
309
# Series/Index with mixed list of Series/list-like
310
+ # s as Series has same index as t -> no warning
311
+ # s as Index is different from t.index -> warning
312
+ if series_or_index == 'series' :
313
+ assert_series_equal (s .str .cat ([t , s .values ]), exp )
314
+ else :
315
+ with tm .assert_produces_warning (expected_warning = FutureWarning ):
316
+ # FutureWarning to switch to alignment by default
317
+ assert_series_or_index_equal (s .str .cat ([t , s .values ]), exp )
318
+
319
+ # Series/Index with mixed list: warning if different indexes
260
320
with tm .assert_produces_warning (expected_warning = FutureWarning ):
261
321
# FutureWarning to switch to alignment by default
262
- assert_series_or_index_equal (s .str .cat ([t . values , s ]), exp )
322
+ assert_series_or_index_equal (s .str .cat ([tt , s . values ]), exp )
263
323
264
- # Series/Index with iterator of list-likes (no warning necessary)
324
+ # Series/Index with iterator of list-likes
265
325
assert_series_or_index_equal (s .str .cat (iter ([t .values , list (s )])), exp )
266
326
267
327
# errors for incorrect lengths
@@ -300,13 +360,13 @@ def test_str_cat_align_indexed(self, series_or_index, join):
300
360
s = Series (['a' , 'b' , 'c' , 'd' ], index = ['a' , 'b' , 'c' , 'd' ])
301
361
t = Series (['D' , 'A' , 'E' , 'B' ], index = ['d' , 'a' , 'e' , 'b' ])
302
362
sa , ta = s .align (t , join = join )
363
+ # result after manual alignment of inputs
364
+ exp = sa .str .cat (ta , na_rep = '-' )
365
+
303
366
if series_or_index == 'index' :
304
367
s = Index (s )
305
368
sa = Index (sa )
306
-
307
- with tm .assert_produces_warning (expected_warning = FutureWarning ):
308
- # result of mamnual alignmnent of inputs
309
- exp = sa .str .cat (ta , na_rep = '-' )
369
+ exp = Index (exp )
310
370
311
371
assert_series_or_index_equal (s .str .cat (t , join = join , na_rep = '-' ), exp )
312
372
@@ -329,6 +389,7 @@ def test_str_cat_align_mixed_inputs(self, join):
329
389
# mixed list of indexed/unindexed
330
390
u = ['A' , 'B' , 'C' , 'D' ]
331
391
exp_outer = Series (['aaA' , 'bbB' , 'c-C' , 'ddD' , '-e-' ])
392
+ # u will be forced have index of s -> use s here as placeholder
332
393
e = concat ([t , s ], axis = 1 , join = (join if join == 'inner' else 'outer' ))
333
394
sa , ea = s .align (e , join = join )
334
395
exp = exp_outer .loc [ea .index ]
@@ -355,6 +416,7 @@ def test_str_cat_special_cases(self):
355
416
mix = [t , t .values , ['A' , 'B' , 'C' , 'D' ], d , d .values ]
356
417
exp = Series (['addAdddd' , 'baaBaaaa' , 'ceeCeeee' , 'dbbDbbbb' ])
357
418
with tm .assert_produces_warning (expected_warning = FutureWarning ):
419
+ # FutureWarning to switch to alignment by default
358
420
tm .assert_series_equal (s .str .cat (mix , join = None ), exp )
359
421
360
422
# lists of elements with different types - aligned with na_rep
@@ -367,7 +429,7 @@ def test_str_cat_special_cases(self):
367
429
tm .assert_series_equal (s .str .cat (iter (mix ), join = 'outer' , na_rep = '-' ),
368
430
exp )
369
431
370
- # right-align with different indexes in other
432
+ # right-align with different indexes in others
371
433
exp = Series (['aa--' , 'd-dd' ], index = [0 , 3 ])
372
434
tm .assert_series_equal (s .str .cat ([t .loc [[0 ]], d .loc [[3 ]]],
373
435
join = 'right' , na_rep = '-' ), exp )
0 commit comments