@@ -409,19 +409,39 @@ def test_replace_literal(any_string_dtype):
409
409
values .str .replace (compiled_pat , "" , regex = False )
410
410
411
411
412
- def test_match ():
412
+ def test_match (any_string_dtype ):
413
413
# New match behavior introduced in 0.13
414
- values = Series (["fooBAD__barBAD" , np .nan , "foo" ])
414
+ expected_dtype = "object" if any_string_dtype == "object" else "boolean"
415
+
416
+ values = Series (["fooBAD__barBAD" , np .nan , "foo" ], dtype = any_string_dtype )
415
417
result = values .str .match (".*(BAD[_]+).*(BAD)" )
416
- exp = Series ([True , np .nan , False ])
417
- tm .assert_series_equal (result , exp )
418
+ expected = Series ([True , np .nan , False ], dtype = expected_dtype )
419
+ tm .assert_series_equal (result , expected )
418
420
419
- values = Series (["fooBAD__barBAD" , "BAD_BADleroybrown" , np .nan , "foo" ])
421
+ values = Series (
422
+ ["fooBAD__barBAD" , "BAD_BADleroybrown" , np .nan , "foo" ], dtype = any_string_dtype
423
+ )
420
424
result = values .str .match (".*BAD[_]+.*BAD" )
421
- exp = Series ([True , True , np .nan , False ])
422
- tm .assert_series_equal (result , exp )
425
+ expected = Series ([True , True , np .nan , False ], dtype = expected_dtype )
426
+ tm .assert_series_equal (result , expected )
423
427
424
- # mixed
428
+ result = values .str .match ("BAD[_]+.*BAD" )
429
+ expected = Series ([False , True , np .nan , False ], dtype = expected_dtype )
430
+ tm .assert_series_equal (result , expected )
431
+
432
+ values = Series (
433
+ ["fooBAD__barBAD" , "^BAD_BADleroybrown" , np .nan , "foo" ], dtype = any_string_dtype
434
+ )
435
+ result = values .str .match ("^BAD[_]+.*BAD" )
436
+ expected = Series ([False , False , np .nan , False ], dtype = expected_dtype )
437
+ tm .assert_series_equal (result , expected )
438
+
439
+ result = values .str .match ("\\ ^BAD[_]+.*BAD" )
440
+ expected = Series ([False , True , np .nan , False ], dtype = expected_dtype )
441
+ tm .assert_series_equal (result , expected )
442
+
443
+
444
+ def test_match_mixed_object ():
425
445
mixed = Series (
426
446
[
427
447
"aBAD_BAD" ,
@@ -435,22 +455,34 @@ def test_match():
435
455
2.0 ,
436
456
]
437
457
)
438
- rs = Series (mixed ).str .match (".*(BAD[_]+).*(BAD)" )
439
- xp = Series ([True , np .nan , True , np .nan , np .nan , False , np .nan , np .nan , np .nan ])
440
- assert isinstance (rs , Series )
441
- tm .assert_series_equal (rs , xp )
458
+ result = Series (mixed ).str .match (".*(BAD[_]+).*(BAD)" )
459
+ expected = Series (
460
+ [True , np .nan , True , np .nan , np .nan , False , np .nan , np .nan , np .nan ]
461
+ )
462
+ assert isinstance (result , Series )
463
+ tm .assert_series_equal (result , expected )
464
+
442
465
443
- # na GH #6609
444
- res = Series (["a" , 0 , np .nan ]).str .match ("a" , na = False )
445
- exp = Series ([True , False , False ])
446
- tm .assert_series_equal (exp , res )
447
- res = Series (["a" , 0 , np .nan ]).str .match ("a" )
448
- exp = Series ([True , np .nan , np .nan ])
449
- tm .assert_series_equal (exp , res )
466
+ def test_match_na_kwarg (any_string_dtype ):
467
+ # GH #6609
468
+ s = Series (["a" , "b" , np .nan ], dtype = any_string_dtype )
450
469
451
- values = Series (["ab" , "AB" , "abc" , "ABC" ])
470
+ result = s .str .match ("a" , na = False )
471
+ expected_dtype = np .bool_ if any_string_dtype == "object" else "boolean"
472
+ expected = Series ([True , False , False ], dtype = expected_dtype )
473
+ tm .assert_series_equal (result , expected )
474
+
475
+ result = s .str .match ("a" )
476
+ expected_dtype = "object" if any_string_dtype == "object" else "boolean"
477
+ expected = Series ([True , False , np .nan ], dtype = expected_dtype )
478
+ tm .assert_series_equal (result , expected )
479
+
480
+
481
+ def test_match_case_kwarg (any_string_dtype ):
482
+ values = Series (["ab" , "AB" , "abc" , "ABC" ], dtype = any_string_dtype )
452
483
result = values .str .match ("ab" , case = False )
453
- expected = Series ([True , True , True , True ])
484
+ expected_dtype = np .bool_ if any_string_dtype == "object" else "boolean"
485
+ expected = Series ([True , True , True , True ], dtype = expected_dtype )
454
486
tm .assert_series_equal (result , expected )
455
487
456
488
0 commit comments