5
5
import operator
6
6
import re
7
7
import unittest
8
+ import warnings
8
9
9
10
import nose
10
11
@@ -392,29 +393,78 @@ def test_repeat(self):
392
393
u ('dddddd' )])
393
394
tm .assert_series_equal (result , exp )
394
395
395
- def test_match (self ):
396
+ def test_deprecated_match (self ):
397
+ # Old match behavior, deprecated (but still default) in 0.13
396
398
values = Series (['fooBAD__barBAD' , NA , 'foo' ])
397
399
398
- result = values .str .match ('.*(BAD[_]+).*(BAD)' )
400
+ with warnings .catch_warnings (record = True ) as w :
401
+ warnings .simplefilter ('always' )
402
+ result = values .str .match ('.*(BAD[_]+).*(BAD)' )
403
+ assert issubclass (w [- 1 ].category , UserWarning )
399
404
exp = Series ([('BAD__' , 'BAD' ), NA , []])
400
405
tm .assert_series_equal (result , exp )
401
406
402
407
# mixed
403
408
mixed = Series (['aBAD_BAD' , NA , 'BAD_b_BAD' , True , datetime .today (),
404
409
'foo' , None , 1 , 2. ])
405
410
406
- rs = Series (mixed ).str .match ('.*(BAD[_]+).*(BAD)' )
411
+ with warnings .catch_warnings (record = True ) as w :
412
+ warnings .simplefilter ('always' )
413
+ rs = Series (mixed ).str .match ('.*(BAD[_]+).*(BAD)' )
414
+ assert issubclass (w [- 1 ].category , UserWarning )
407
415
xp = [('BAD_' , 'BAD' ), NA , ('BAD_' , 'BAD' ), NA , NA , [], NA , NA , NA ]
408
416
tm .assert_isinstance (rs , Series )
409
417
tm .assert_almost_equal (rs , xp )
410
418
411
419
# unicode
412
420
values = Series ([u ('fooBAD__barBAD' ), NA , u ('foo' )])
413
421
414
- result = values .str .match ('.*(BAD[_]+).*(BAD)' )
422
+ with warnings .catch_warnings (record = True ) as w :
423
+ warnings .simplefilter ('always' )
424
+ result = values .str .match ('.*(BAD[_]+).*(BAD)' )
425
+ assert issubclass (w [- 1 ].category , UserWarning )
415
426
exp = Series ([(u ('BAD__' ), u ('BAD' )), NA , []])
416
427
tm .assert_series_equal (result , exp )
417
428
429
+ def test_match (self ):
430
+ # New match behavior introduced in 0.13
431
+ values = Series (['fooBAD__barBAD' , NA , 'foo' ])
432
+ with warnings .catch_warnings (record = True ) as w :
433
+ warnings .simplefilter ('always' )
434
+ result = values .str .match ('.*(BAD[_]+).*(BAD)' , as_indexer = True )
435
+ assert issubclass (w [- 1 ].category , UserWarning )
436
+ exp = Series ([True , NA , False ])
437
+ tm .assert_series_equal (result , exp )
438
+
439
+ # If no groups, use new behavior even when as_indexer is False.
440
+ # (Old behavior is pretty much useless in this case.)
441
+ values = Series (['fooBAD__barBAD' , NA , 'foo' ])
442
+ result = values .str .match ('.*BAD[_]+.*BAD' , as_indexer = False )
443
+ exp = Series ([True , NA , False ])
444
+ tm .assert_series_equal (result , exp )
445
+
446
+ # mixed
447
+ mixed = Series (['aBAD_BAD' , NA , 'BAD_b_BAD' , True , datetime .today (),
448
+ 'foo' , None , 1 , 2. ])
449
+
450
+ with warnings .catch_warnings (record = True ) as w :
451
+ warnings .simplefilter ('always' )
452
+ rs = Series (mixed ).str .match ('.*(BAD[_]+).*(BAD)' , as_indexer = True )
453
+ assert issubclass (w [- 1 ].category , UserWarning )
454
+ xp = [True , NA , True , NA , NA , False , NA , NA , NA ]
455
+ tm .assert_isinstance (rs , Series )
456
+ tm .assert_almost_equal (rs , xp )
457
+
458
+ # unicode
459
+ values = Series ([u ('fooBAD__barBAD' ), NA , u ('foo' )])
460
+
461
+ with warnings .catch_warnings (record = True ) as w :
462
+ warnings .simplefilter ('always' )
463
+ result = values .str .match ('.*(BAD[_]+).*(BAD)' , as_indexer = True )
464
+ assert issubclass (w [- 1 ].category , UserWarning )
465
+ exp = Series ([True , NA , False ])
466
+ tm .assert_series_equal (result , exp )
467
+
418
468
def test_extract (self ):
419
469
# Contains tests like those in test_match and some others.
420
470
@@ -966,7 +1016,10 @@ def test_match_findall_flags(self):
966
1016
967
1017
pat = pattern = r'([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})'
968
1018
969
- result = data .str .match (pat , flags = re .IGNORECASE )
1019
+ with warnings .catch_warnings (record = True ) as w :
1020
+ warnings .simplefilter ('always' )
1021
+ result = data .str .match (pat , flags = re .IGNORECASE )
1022
+ assert issubclass (w [- 1 ].category , UserWarning )
970
1023
self .assertEquals (result [0 ], ('dave' , 'google' , 'com' ))
971
1024
972
1025
result = data .str .findall (pat , flags = re .IGNORECASE )
@@ -975,7 +1028,10 @@ def test_match_findall_flags(self):
975
1028
result = data .str .count (pat , flags = re .IGNORECASE )
976
1029
self .assertEquals (result [0 ], 1 )
977
1030
978
- result = data .str .contains (pat , flags = re .IGNORECASE )
1031
+ with warnings .catch_warnings (record = True ) as w :
1032
+ warnings .simplefilter ('always' )
1033
+ result = data .str .contains (pat , flags = re .IGNORECASE )
1034
+ assert issubclass (w [- 1 ].category , UserWarning )
979
1035
self .assertEquals (result [0 ], True )
980
1036
981
1037
def test_encode_decode (self ):
0 commit comments