55import operator
66import re
77import unittest
8+ import warnings
89
910import nose
1011
@@ -392,29 +393,78 @@ def test_repeat(self):
392393 u ('dddddd' )])
393394 tm .assert_series_equal (result , exp )
394395
395- def test_match (self ):
396+ def test_deprecated_match (self ):
397+ # Old match behavior, deprecated (but still default) in 0.13
396398 values = Series (['fooBAD__barBAD' , NA , 'foo' ])
397399
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 )
399404 exp = Series ([('BAD__' , 'BAD' ), NA , []])
400405 tm .assert_series_equal (result , exp )
401406
402407 # mixed
403408 mixed = Series (['aBAD_BAD' , NA , 'BAD_b_BAD' , True , datetime .today (),
404409 'foo' , None , 1 , 2. ])
405410
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 )
407415 xp = [('BAD_' , 'BAD' ), NA , ('BAD_' , 'BAD' ), NA , NA , [], NA , NA , NA ]
408416 tm .assert_isinstance (rs , Series )
409417 tm .assert_almost_equal (rs , xp )
410418
411419 # unicode
412420 values = Series ([u ('fooBAD__barBAD' ), NA , u ('foo' )])
413421
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 )
415426 exp = Series ([(u ('BAD__' ), u ('BAD' )), NA , []])
416427 tm .assert_series_equal (result , exp )
417428
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+
418468 def test_extract (self ):
419469 # Contains tests like those in test_match and some others.
420470
@@ -966,7 +1016,10 @@ def test_match_findall_flags(self):
9661016
9671017 pat = pattern = r'([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})'
9681018
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 )
9701023 self .assertEquals (result [0 ], ('dave' , 'google' , 'com' ))
9711024
9721025 result = data .str .findall (pat , flags = re .IGNORECASE )
@@ -975,7 +1028,10 @@ def test_match_findall_flags(self):
9751028 result = data .str .count (pat , flags = re .IGNORECASE )
9761029 self .assertEquals (result [0 ], 1 )
9771030
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 )
9791035 self .assertEquals (result [0 ], True )
9801036
9811037 def test_encode_decode (self ):
0 commit comments