@@ -137,15 +137,11 @@ def test_repeat_mixed_object():
137
137
tm .assert_series_equal (result , expected )
138
138
139
139
140
- def test_repeat_with_null (any_string_dtype ):
140
+ @pytest .mark .parametrize ("arg, repeat" , [[None , 4 ], ["b" , None ]])
141
+ def test_repeat_with_null (any_string_dtype , arg , repeat ):
141
142
# GH: 31632
142
- ser = Series (["a" , None ], dtype = any_string_dtype )
143
- result = ser .str .repeat ([3 , 4 ])
144
- expected = Series (["aaa" , np .nan ], dtype = any_string_dtype )
145
- tm .assert_series_equal (result , expected )
146
-
147
- ser = Series (["a" , "b" ], dtype = any_string_dtype )
148
- result = ser .str .repeat ([3 , None ])
143
+ ser = Series (["a" , arg ], dtype = any_string_dtype )
144
+ result = ser .str .repeat ([3 , repeat ])
149
145
expected = Series (["aaa" , np .nan ], dtype = any_string_dtype )
150
146
tm .assert_series_equal (result , expected )
151
147
@@ -397,27 +393,28 @@ def test_index_not_found_raises(index_or_series, any_string_dtype):
397
393
obj .str .index ("DE" )
398
394
399
395
400
- def test_index_wrong_type_raises (index_or_series , any_string_dtype ):
396
+ @pytest .mark .parametrize ("method" , ["index" , "rindex" ])
397
+ def test_index_wrong_type_raises (index_or_series , any_string_dtype , method ):
401
398
obj = index_or_series ([], dtype = any_string_dtype )
402
399
msg = "expected a string object, not int"
403
400
404
401
with pytest .raises (TypeError , match = msg ):
405
- obj .str .index (0 )
406
-
407
- with pytest .raises (TypeError , match = msg ):
408
- obj .str .rindex (0 )
402
+ getattr (obj .str , method )(0 )
409
403
410
404
411
- def test_index_missing (any_string_dtype ):
405
+ @pytest .mark .parametrize (
406
+ "method, exp" ,
407
+ [
408
+ ["index" , [1 , 1 , 0 ]],
409
+ ["rindex" , [3 , 1 , 2 ]],
410
+ ],
411
+ )
412
+ def test_index_missing (any_string_dtype , method , exp ):
412
413
ser = Series (["abcb" , "ab" , "bcbe" , np .nan ], dtype = any_string_dtype )
413
414
expected_dtype = np .float64 if any_string_dtype == "object" else "Int64"
414
415
415
- result = ser .str .index ("b" )
416
- expected = Series ([1 , 1 , 0 , np .nan ], dtype = expected_dtype )
417
- tm .assert_series_equal (result , expected )
418
-
419
- result = ser .str .rindex ("b" )
420
- expected = Series ([3 , 1 , 2 , np .nan ], dtype = expected_dtype )
416
+ result = getattr (ser .str , method )("b" )
417
+ expected = Series (exp + [np .nan ], dtype = expected_dtype )
421
418
tm .assert_series_equal (result , expected )
422
419
423
420
@@ -488,53 +485,51 @@ def test_slice_replace(start, stop, repl, expected, any_string_dtype):
488
485
tm .assert_series_equal (result , expected )
489
486
490
487
491
- def test_strip_lstrip_rstrip (any_string_dtype ):
488
+ @pytest .mark .parametrize (
489
+ "method, exp" ,
490
+ [
491
+ ["strip" , ["aa" , "bb" , np .nan , "cc" ]],
492
+ ["lstrip" , ["aa " , "bb \n " , np .nan , "cc " ]],
493
+ ["rstrip" , [" aa" , " bb" , np .nan , "cc" ]],
494
+ ],
495
+ )
496
+ def test_strip_lstrip_rstrip (any_string_dtype , method , exp ):
492
497
ser = Series ([" aa " , " bb \n " , np .nan , "cc " ], dtype = any_string_dtype )
493
498
494
- result = ser .str .strip ()
495
- expected = Series (["aa" , "bb" , np .nan , "cc" ], dtype = any_string_dtype )
496
- tm .assert_series_equal (result , expected )
497
-
498
- result = ser .str .lstrip ()
499
- expected = Series (["aa " , "bb \n " , np .nan , "cc " ], dtype = any_string_dtype )
500
- tm .assert_series_equal (result , expected )
501
-
502
- result = ser .str .rstrip ()
503
- expected = Series ([" aa" , " bb" , np .nan , "cc" ], dtype = any_string_dtype )
499
+ result = getattr (ser .str , method )()
500
+ expected = Series (exp , dtype = any_string_dtype )
504
501
tm .assert_series_equal (result , expected )
505
502
506
503
507
- def test_strip_lstrip_rstrip_mixed_object ():
504
+ @pytest .mark .parametrize (
505
+ "method, exp" ,
506
+ [
507
+ ["strip" , ["aa" , np .nan , "bb" ]],
508
+ ["lstrip" , ["aa " , np .nan , "bb \t \n " ]],
509
+ ["rstrip" , [" aa" , np .nan , " bb" ]],
510
+ ],
511
+ )
512
+ def test_strip_lstrip_rstrip_mixed_object (method , exp ):
508
513
ser = Series ([" aa " , np .nan , " bb \t \n " , True , datetime .today (), None , 1 , 2.0 ])
509
514
510
- result = ser .str .strip ()
511
- expected = Series (["aa" , np .nan , "bb" , np .nan , np .nan , np .nan , np .nan , np .nan ])
512
- tm .assert_series_equal (result , expected )
513
-
514
- result = ser .str .lstrip ()
515
- expected = Series (
516
- ["aa " , np .nan , "bb \t \n " , np .nan , np .nan , np .nan , np .nan , np .nan ]
517
- )
518
- tm .assert_series_equal (result , expected )
519
-
520
- result = ser .str .rstrip ()
521
- expected = Series ([" aa" , np .nan , " bb" , np .nan , np .nan , np .nan , np .nan , np .nan ])
515
+ result = getattr (ser .str , method )()
516
+ expected = Series (exp + [np .nan , np .nan , np .nan , np .nan , np .nan ])
522
517
tm .assert_series_equal (result , expected )
523
518
524
519
525
- def test_strip_lstrip_rstrip_args (any_string_dtype ):
520
+ @pytest .mark .parametrize (
521
+ "method, exp" ,
522
+ [
523
+ ["strip" , ["ABC" , " BNSD" , "LDFJH " ]],
524
+ ["lstrip" , ["ABCxx" , " BNSD" , "LDFJH xx" ]],
525
+ ["rstrip" , ["xxABC" , "xx BNSD" , "LDFJH " ]],
526
+ ],
527
+ )
528
+ def test_strip_lstrip_rstrip_args (any_string_dtype , method , exp ):
526
529
ser = Series (["xxABCxx" , "xx BNSD" , "LDFJH xx" ], dtype = any_string_dtype )
527
530
528
- result = ser .str .strip ("x" )
529
- expected = Series (["ABC" , " BNSD" , "LDFJH " ], dtype = any_string_dtype )
530
- tm .assert_series_equal (result , expected )
531
-
532
- result = ser .str .lstrip ("x" )
533
- expected = Series (["ABCxx" , " BNSD" , "LDFJH xx" ], dtype = any_string_dtype )
534
- tm .assert_series_equal (result , expected )
535
-
536
- result = ser .str .rstrip ("x" )
537
- expected = Series (["xxABC" , "xx BNSD" , "LDFJH " ], dtype = any_string_dtype )
531
+ result = getattr (ser .str , method )("x" )
532
+ expected = Series (exp , dtype = any_string_dtype )
538
533
tm .assert_series_equal (result , expected )
539
534
540
535
0 commit comments