@@ -232,20 +232,25 @@ def str_count(arr, pat):
232
232
return _na_map (f , arr )
233
233
234
234
235
- def str_contains (arr , pat ):
235
+ def str_contains (arr , pat , case = True ):
236
236
"""
237
237
Check whether given pattern is contained in each string in the array
238
238
239
239
Parameters
240
240
----------
241
241
pat : string
242
242
Character sequence or regular expression
243
+ case : boolean, default True
244
+ If True, case sensitive
243
245
244
246
Returns
245
247
-------
246
248
247
249
"""
248
- regex = re .compile (pat )
250
+ if not case :
251
+ regex = re .compile (pat , re .IGNORECASE )
252
+ else :
253
+ regex = re .compile (pat )
249
254
f = lambda x : bool (regex .search (x ))
250
255
return _na_map (f , arr )
251
256
@@ -308,7 +313,7 @@ def str_upper(arr):
308
313
return _na_map (lambda x : x .upper (), arr )
309
314
310
315
311
- def str_replace (arr , pat , repl , n = 0 ):
316
+ def str_replace (arr , pat , repl , n = 0 , case = True ):
312
317
"""
313
318
Replace
314
319
@@ -320,12 +325,17 @@ def str_replace(arr, pat, repl, n=0):
320
325
Replacement sequence
321
326
n : int, default 0 (all)
322
327
Number of replacements to make from start
328
+ case : boolean, default True
329
+ If True, case sensitive
323
330
324
331
Returns
325
332
-------
326
333
replaced : array
327
334
"""
328
- regex = re .compile (pat )
335
+ if not case :
336
+ regex = re .compile (pat , re .IGNORECASE )
337
+ else :
338
+ regex = re .compile (pat )
329
339
def f (x ):
330
340
return regex .sub (repl , x , count = n )
331
341
@@ -599,7 +609,8 @@ def wrapper(self):
599
609
return self ._wrap_result (result )
600
610
601
611
wrapper .__name__ = f .__name__
602
- wrapper .__doc__ = f .__doc__
612
+ if f .__doc__ :
613
+ wrapper .__doc__ = f .__doc__
603
614
604
615
return wrapper
605
616
@@ -610,10 +621,20 @@ def wrapper(self, pat):
610
621
return self ._wrap_result (result )
611
622
612
623
wrapper .__name__ = f .__name__
613
- wrapper .__doc__ = f .__doc__
624
+ if f .__doc__ :
625
+ wrapper .__doc__ = f .__doc__
614
626
615
627
return wrapper
616
628
629
+ def copy (source ):
630
+ "Copy a docstring from another source function (if present)"
631
+ def do_copy (target ):
632
+ if source .__doc__ :
633
+ target .__doc__ = source .__doc__
634
+ return target
635
+ return do_copy
636
+
637
+
617
638
class StringMethods (object ):
618
639
"""
619
640
Vectorized string functions for Series. NAs stay NA unless handled
@@ -632,47 +653,61 @@ def _wrap_result(self, result):
632
653
return Series (result , index = self .series .index ,
633
654
name = self .series .name )
634
655
656
+ @copy (str_cat )
635
657
def cat (self , others = None , sep = None , na_rep = None ):
636
658
result = str_cat (self .series , others = others , sep = sep , na_rep = na_rep )
637
659
return self ._wrap_result (result )
638
660
661
+ @copy (str_split )
639
662
def split (self , pat , n = 0 ):
640
663
result = str_split (self .series , pat , n = n )
641
664
return self ._wrap_result (result )
642
665
666
+ @copy (str_get )
643
667
def get (self , i ):
644
668
result = str_get (self .series , i )
645
669
return self ._wrap_result (result )
646
670
671
+ @copy (str_join )
647
672
def join (self , sep ):
648
673
result = str_join (self .series , sep )
649
674
return self ._wrap_result (result )
650
675
651
- def replace (self , pat , repl , n = 0 ):
652
- result = str_replace (self .series , pat , repl , n = n )
676
+ @copy (str_contains )
677
+ def contains (self , pat , case = True ):
678
+ result = str_contains (self .series , pat , case = case )
679
+ return self ._wrap_result (result )
680
+
681
+ @copy (str_replace )
682
+ def replace (self , pat , repl , n = 0 , case = True ):
683
+ result = str_replace (self .series , pat , repl , n = n , case = case )
653
684
return self ._wrap_result (result )
654
685
686
+ @copy (str_repeat )
655
687
def repeat (self , repeats ):
656
688
result = str_repeat (self .series , repeats )
657
689
return self ._wrap_result (result )
658
690
691
+ @copy (str_pad )
659
692
def pad (self , width , side = 'left' ):
660
693
result = str_pad (self .series , width , side = side )
661
694
return self ._wrap_result (result )
662
695
696
+ @copy (str_center )
663
697
def center (self , width ):
664
698
result = str_center (self .series , width )
665
699
return self ._wrap_result (result )
666
700
701
+ @copy (str_slice )
667
702
def slice (self , start = None , stop = None ):
668
703
result = str_slice (self .series , start , stop )
669
704
return self ._wrap_result (result )
670
705
706
+ @copy (str_slice )
671
707
def slice_replace (self , i = None , j = None ):
672
708
raise NotImplementedError
673
709
674
710
count = _pat_wrapper (str_count )
675
- contains = _pat_wrapper (str_contains )
676
711
startswith = _pat_wrapper (str_startswith )
677
712
endswith = _pat_wrapper (str_endswith )
678
713
findall = _pat_wrapper (str_findall )
0 commit comments