@@ -137,15 +137,11 @@ def test_repeat_mixed_object():
137137 tm .assert_series_equal (result , expected )
138138
139139
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 ):
141142 # 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 ])
149145 expected = Series (["aaa" , np .nan ], dtype = any_string_dtype )
150146 tm .assert_series_equal (result , expected )
151147
@@ -397,27 +393,28 @@ def test_index_not_found_raises(index_or_series, any_string_dtype):
397393 obj .str .index ("DE" )
398394
399395
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 ):
401398 obj = index_or_series ([], dtype = any_string_dtype )
402399 msg = "expected a string object, not int"
403400
404401 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 )
409403
410404
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 ):
412413 ser = Series (["abcb" , "ab" , "bcbe" , np .nan ], dtype = any_string_dtype )
413414 expected_dtype = np .float64 if any_string_dtype == "object" else "Int64"
414415
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 )
421418 tm .assert_series_equal (result , expected )
422419
423420
@@ -488,53 +485,51 @@ def test_slice_replace(start, stop, repl, expected, any_string_dtype):
488485 tm .assert_series_equal (result , expected )
489486
490487
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 ):
492497 ser = Series ([" aa " , " bb \n " , np .nan , "cc " ], dtype = any_string_dtype )
493498
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 )
504501 tm .assert_series_equal (result , expected )
505502
506503
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 ):
508513 ser = Series ([" aa " , np .nan , " bb \t \n " , True , datetime .today (), None , 1 , 2.0 ])
509514
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 ])
522517 tm .assert_series_equal (result , expected )
523518
524519
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 ):
526529 ser = Series (["xxABCxx" , "xx BNSD" , "LDFJH xx" ], dtype = any_string_dtype )
527530
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 )
538533 tm .assert_series_equal (result , expected )
539534
540535
0 commit comments