@@ -335,90 +335,90 @@ def test_split_with_name():
335
335
tm .assert_index_equal (res , exp )
336
336
337
337
338
- def test_partition_series ():
338
+ def test_partition_series (any_string_dtype ):
339
339
# https://github.com/pandas-dev/pandas/issues/23558
340
340
341
- values = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" , None ])
341
+ s = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" , None ], dtype = any_string_dtype )
342
342
343
- result = values .str .partition ("_" , expand = False )
344
- exp = Series (
343
+ result = s .str .partition ("_" , expand = False )
344
+ expected = Series (
345
345
[("a" , "_" , "b_c" ), ("c" , "_" , "d_e" ), np .nan , ("f" , "_" , "g_h" ), None ]
346
346
)
347
- tm .assert_series_equal (result , exp )
347
+ tm .assert_series_equal (result , expected )
348
348
349
- result = values .str .rpartition ("_" , expand = False )
350
- exp = Series (
349
+ result = s .str .rpartition ("_" , expand = False )
350
+ expected = Series (
351
351
[("a_b" , "_" , "c" ), ("c_d" , "_" , "e" ), np .nan , ("f_g" , "_" , "h" ), None ]
352
352
)
353
- tm .assert_series_equal (result , exp )
353
+ tm .assert_series_equal (result , expected )
354
354
355
355
# more than one char
356
- values = Series (["a__b__c" , "c__d__e" , np .nan , "f__g__h" , None ])
357
- result = values .str .partition ("__" , expand = False )
358
- exp = Series (
356
+ s = Series (["a__b__c" , "c__d__e" , np .nan , "f__g__h" , None ])
357
+ result = s .str .partition ("__" , expand = False )
358
+ expected = Series (
359
359
[
360
360
("a" , "__" , "b__c" ),
361
361
("c" , "__" , "d__e" ),
362
362
np .nan ,
363
363
("f" , "__" , "g__h" ),
364
364
None ,
365
- ]
365
+ ],
366
366
)
367
- tm .assert_series_equal (result , exp )
367
+ tm .assert_series_equal (result , expected )
368
368
369
- result = values .str .rpartition ("__" , expand = False )
370
- exp = Series (
369
+ result = s .str .rpartition ("__" , expand = False )
370
+ expected = Series (
371
371
[
372
372
("a__b" , "__" , "c" ),
373
373
("c__d" , "__" , "e" ),
374
374
np .nan ,
375
375
("f__g" , "__" , "h" ),
376
376
None ,
377
- ]
377
+ ],
378
378
)
379
- tm .assert_series_equal (result , exp )
379
+ tm .assert_series_equal (result , expected )
380
380
381
381
# None
382
- values = Series (["a b c" , "c d e" , np .nan , "f g h" , None ])
383
- result = values .str .partition (expand = False )
384
- exp = Series (
382
+ s = Series (["a b c" , "c d e" , np .nan , "f g h" , None ], dtype = any_string_dtype )
383
+ result = s .str .partition (expand = False )
384
+ expected = Series (
385
385
[("a" , " " , "b c" ), ("c" , " " , "d e" ), np .nan , ("f" , " " , "g h" ), None ]
386
386
)
387
- tm .assert_series_equal (result , exp )
387
+ tm .assert_series_equal (result , expected )
388
388
389
- result = values .str .rpartition (expand = False )
390
- exp = Series (
389
+ result = s .str .rpartition (expand = False )
390
+ expected = Series (
391
391
[("a b" , " " , "c" ), ("c d" , " " , "e" ), np .nan , ("f g" , " " , "h" ), None ]
392
392
)
393
- tm .assert_series_equal (result , exp )
393
+ tm .assert_series_equal (result , expected )
394
394
395
395
# Not split
396
- values = Series (["abc" , "cde" , np .nan , "fgh" , None ])
397
- result = values .str .partition ("_" , expand = False )
398
- exp = Series ([("abc" , "" , "" ), ("cde" , "" , "" ), np .nan , ("fgh" , "" , "" ), None ])
399
- tm .assert_series_equal (result , exp )
396
+ s = Series (["abc" , "cde" , np .nan , "fgh" , None ], dtype = any_string_dtype )
397
+ result = s .str .partition ("_" , expand = False )
398
+ expected = Series ([("abc" , "" , "" ), ("cde" , "" , "" ), np .nan , ("fgh" , "" , "" ), None ])
399
+ tm .assert_series_equal (result , expected )
400
400
401
- result = values .str .rpartition ("_" , expand = False )
402
- exp = Series ([("" , "" , "abc" ), ("" , "" , "cde" ), np .nan , ("" , "" , "fgh" ), None ])
403
- tm .assert_series_equal (result , exp )
401
+ result = s .str .rpartition ("_" , expand = False )
402
+ expected = Series ([("" , "" , "abc" ), ("" , "" , "cde" ), np .nan , ("" , "" , "fgh" ), None ])
403
+ tm .assert_series_equal (result , expected )
404
404
405
405
# unicode
406
- values = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" ])
406
+ s = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" ], dtype = any_string_dtype )
407
407
408
- result = values .str .partition ("_" , expand = False )
409
- exp = Series ([("a" , "_" , "b_c" ), ("c" , "_" , "d_e" ), np .nan , ("f" , "_" , "g_h" )])
410
- tm .assert_series_equal (result , exp )
408
+ result = s .str .partition ("_" , expand = False )
409
+ expected = Series ([("a" , "_" , "b_c" ), ("c" , "_" , "d_e" ), np .nan , ("f" , "_" , "g_h" )])
410
+ tm .assert_series_equal (result , expected )
411
411
412
- result = values .str .rpartition ("_" , expand = False )
413
- exp = Series ([("a_b" , "_" , "c" ), ("c_d" , "_" , "e" ), np .nan , ("f_g" , "_" , "h" )])
414
- tm .assert_series_equal (result , exp )
412
+ result = s .str .rpartition ("_" , expand = False )
413
+ expected = Series ([("a_b" , "_" , "c" ), ("c_d" , "_" , "e" ), np .nan , ("f_g" , "_" , "h" )])
414
+ tm .assert_series_equal (result , expected )
415
415
416
416
# compare to standard lib
417
- values = Series (["A_B_C" , "B_C_D" , "E_F_G" , "EFGHEF" ])
418
- result = values .str .partition ("_" , expand = False ).tolist ()
419
- assert result == [v .partition ("_" ) for v in values ]
420
- result = values .str .rpartition ("_" , expand = False ).tolist ()
421
- assert result == [v .rpartition ("_" ) for v in values ]
417
+ s = Series (["A_B_C" , "B_C_D" , "E_F_G" , "EFGHEF" ], dtype = any_string_dtype )
418
+ result = s .str .partition ("_" , expand = False ).tolist ()
419
+ assert result == [v .partition ("_" ) for v in s ]
420
+ result = s .str .rpartition ("_" , expand = False ).tolist ()
421
+ assert result == [v .rpartition ("_" ) for v in s ]
422
422
423
423
424
424
def test_partition_index ():
@@ -475,88 +475,96 @@ def test_partition_index():
475
475
assert result .nlevels == 3
476
476
477
477
478
- def test_partition_to_dataframe ():
478
+ def test_partition_to_dataframe (any_string_dtype ):
479
479
# https://github.com/pandas-dev/pandas/issues/23558
480
480
481
- values = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" , None ])
482
- result = values .str .partition ("_" )
483
- exp = DataFrame (
481
+ s = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" , None ], dtype = any_string_dtype )
482
+ result = s .str .partition ("_" )
483
+ expected = DataFrame (
484
484
{
485
485
0 : ["a" , "c" , np .nan , "f" , None ],
486
486
1 : ["_" , "_" , np .nan , "_" , None ],
487
487
2 : ["b_c" , "d_e" , np .nan , "g_h" , None ],
488
- }
488
+ },
489
+ dtype = any_string_dtype ,
489
490
)
490
- tm .assert_frame_equal (result , exp )
491
+ tm .assert_frame_equal (result , expected )
491
492
492
- result = values .str .rpartition ("_" )
493
- exp = DataFrame (
493
+ result = s .str .rpartition ("_" )
494
+ expected = DataFrame (
494
495
{
495
496
0 : ["a_b" , "c_d" , np .nan , "f_g" , None ],
496
497
1 : ["_" , "_" , np .nan , "_" , None ],
497
498
2 : ["c" , "e" , np .nan , "h" , None ],
498
- }
499
+ },
500
+ dtype = any_string_dtype ,
499
501
)
500
- tm .assert_frame_equal (result , exp )
502
+ tm .assert_frame_equal (result , expected )
501
503
502
- values = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" , None ])
503
- result = values .str .partition ("_" , expand = True )
504
- exp = DataFrame (
504
+ s = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" , None ], dtype = any_string_dtype )
505
+ result = s .str .partition ("_" , expand = True )
506
+ expected = DataFrame (
505
507
{
506
508
0 : ["a" , "c" , np .nan , "f" , None ],
507
509
1 : ["_" , "_" , np .nan , "_" , None ],
508
510
2 : ["b_c" , "d_e" , np .nan , "g_h" , None ],
509
- }
511
+ },
512
+ dtype = any_string_dtype ,
510
513
)
511
- tm .assert_frame_equal (result , exp )
514
+ tm .assert_frame_equal (result , expected )
512
515
513
- result = values .str .rpartition ("_" , expand = True )
514
- exp = DataFrame (
516
+ result = s .str .rpartition ("_" , expand = True )
517
+ expected = DataFrame (
515
518
{
516
519
0 : ["a_b" , "c_d" , np .nan , "f_g" , None ],
517
520
1 : ["_" , "_" , np .nan , "_" , None ],
518
521
2 : ["c" , "e" , np .nan , "h" , None ],
519
- }
522
+ },
523
+ dtype = any_string_dtype ,
520
524
)
521
- tm .assert_frame_equal (result , exp )
525
+ tm .assert_frame_equal (result , expected )
522
526
523
527
524
- def test_partition_with_name ():
528
+ def test_partition_with_name (any_string_dtype ):
525
529
# GH 12617
526
530
527
- s = Series (["a,b" , "c,d" ], name = "xxx" )
528
- res = s .str .partition ("," )
529
- exp = DataFrame ({0 : ["a" , "c" ], 1 : ["," , "," ], 2 : ["b" , "d" ]})
530
- tm .assert_frame_equal (res , exp )
531
+ s = Series (["a,b" , "c,d" ], name = "xxx" , dtype = any_string_dtype )
532
+ result = s .str .partition ("," )
533
+ expected = DataFrame (
534
+ {0 : ["a" , "c" ], 1 : ["," , "," ], 2 : ["b" , "d" ]}, dtype = any_string_dtype
535
+ )
536
+ tm .assert_frame_equal (result , expected )
531
537
532
538
# should preserve name
533
- res = s .str .partition ("," , expand = False )
534
- exp = Series ([("a" , "," , "b" ), ("c" , "," , "d" )], name = "xxx" )
535
- tm .assert_series_equal (res , exp )
539
+ result = s .str .partition ("," , expand = False )
540
+ expected = Series ([("a" , "," , "b" ), ("c" , "," , "d" )], name = "xxx" )
541
+ tm .assert_series_equal (result , expected )
542
+
536
543
544
+ def test_partition_index_with_name ():
537
545
idx = Index (["a,b" , "c,d" ], name = "xxx" )
538
- res = idx .str .partition ("," )
539
- exp = MultiIndex .from_tuples ([("a" , "," , "b" ), ("c" , "," , "d" )])
540
- assert res .nlevels == 3
541
- tm .assert_index_equal (res , exp )
546
+ result = idx .str .partition ("," )
547
+ expected = MultiIndex .from_tuples ([("a" , "," , "b" ), ("c" , "," , "d" )])
548
+ assert result .nlevels == 3
549
+ tm .assert_index_equal (result , expected )
542
550
543
551
# should preserve name
544
- res = idx .str .partition ("," , expand = False )
545
- exp = Index (np .array ([("a" , "," , "b" ), ("c" , "," , "d" )]), name = "xxx" )
546
- assert res .nlevels == 1
547
- tm .assert_index_equal (res , exp )
552
+ result = idx .str .partition ("," , expand = False )
553
+ expected = Index (np .array ([("a" , "," , "b" ), ("c" , "," , "d" )]), name = "xxx" )
554
+ assert result .nlevels == 1
555
+ tm .assert_index_equal (result , expected )
548
556
549
557
550
- def test_partition_sep_kwarg ():
558
+ def test_partition_sep_kwarg (any_string_dtype ):
551
559
# GH 22676; depr kwarg "pat" in favor of "sep"
552
- values = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" ])
560
+ s = Series (["a_b_c" , "c_d_e" , np .nan , "f_g_h" ], dtype = any_string_dtype )
553
561
554
- expected = values .str .partition (sep = "_" )
555
- result = values .str .partition ("_" )
562
+ expected = s .str .partition (sep = "_" )
563
+ result = s .str .partition ("_" )
556
564
tm .assert_frame_equal (result , expected )
557
565
558
- expected = values .str .rpartition (sep = "_" )
559
- result = values .str .rpartition ("_" )
566
+ expected = s .str .rpartition (sep = "_" )
567
+ result = s .str .rpartition ("_" )
560
568
tm .assert_frame_equal (result , expected )
561
569
562
570
0 commit comments