@@ -78,14 +78,10 @@ def get(self, i, default=None):
78
78
items : array of objects
79
79
'''
80
80
if default is None :
81
- default = ''
81
+ default = self . _obj . dtype . type ( '' )
82
82
83
- def f (x ):
84
- n = len (x )
85
- if n <= i or i < - n :
86
- return default
87
- return x [i ]
88
- return self ._apply (f )
83
+ obj = slice (i , i + 1 )
84
+ return self ._apply (lambda x : x [obj ])
89
85
90
86
def slice (self , start = None , stop = None , step = None ):
91
87
'''
@@ -130,12 +126,14 @@ def slice_replace(self, start=None, stop=None, repl=''):
130
126
-------
131
127
replaced : same type as values
132
128
'''
129
+ repl = self ._obj .dtype .type (repl )
130
+
133
131
def f (x ):
134
- if x [start :stop ] == '' :
132
+ if len ( x [start :stop ]) == 0 :
135
133
local_stop = start
136
134
else :
137
135
local_stop = stop
138
- y = ''
136
+ y = self . _obj . dtype . type ( '' )
139
137
if start is not None :
140
138
y += x [:start ]
141
139
y += repl
@@ -314,6 +312,7 @@ def count(self, pat, flags=0):
314
312
-------
315
313
counts : array of int
316
314
'''
315
+ pat = self ._obj .dtype .type (pat )
317
316
regex = re .compile (pat , flags = flags )
318
317
f = lambda x : len (regex .findall (x ))
319
318
return self ._apply (f , dtype = int )
@@ -333,6 +332,7 @@ def startswith(self, pat):
333
332
An array of booleans indicating whether the given pattern matches
334
333
the start of each string element.
335
334
'''
335
+ pat = self ._obj .dtype .type (pat )
336
336
f = lambda x : x .startswith (pat )
337
337
return self ._apply (f , dtype = bool )
338
338
@@ -351,6 +351,7 @@ def endswith(self, pat):
351
351
A Series of booleans indicating whether the given pattern matches
352
352
the end of each string element.
353
353
'''
354
+ pat = self ._obj .dtype .type (pat )
354
355
f = lambda x : x .endswith (pat )
355
356
return self ._apply (f , dtype = bool )
356
357
@@ -374,7 +375,7 @@ def pad(self, width, side='left', fillchar=' '):
374
375
Array with a minimum number of char in each element.
375
376
'''
376
377
width = int (width )
377
- fillchar = str (fillchar )
378
+ fillchar = self . _obj . dtype . type (fillchar )
378
379
if len (fillchar ) != 1 :
379
380
raise TypeError ('fillchar must be a character, not str' )
380
381
@@ -491,6 +492,7 @@ def contains(self, pat, case=True, flags=0, regex=True):
491
492
given pattern is contained within the string of each element
492
493
of the array.
493
494
'''
495
+ pat = self ._obj .dtype .type (pat )
494
496
if regex :
495
497
if not case :
496
498
flags |= re .IGNORECASE
@@ -530,6 +532,7 @@ def match(self, pat, case=True, flags=0):
530
532
if not case :
531
533
flags |= re .IGNORECASE
532
534
535
+ pat = self ._obj .dtype .type (pat )
533
536
regex = re .compile (pat , flags = flags )
534
537
f = lambda x : bool (regex .match (x ))
535
538
return self ._apply (f , dtype = bool )
@@ -554,6 +557,9 @@ def strip(self, to_strip=None, side='both'):
554
557
-------
555
558
stripped : same type as values
556
559
'''
560
+ if to_strip is not None :
561
+ to_strip = self ._obj .dtype .type (to_strip )
562
+
557
563
if side == 'both' :
558
564
f = lambda x : x .strip (to_strip )
559
565
elif side == 'left' :
@@ -703,7 +709,8 @@ def find(self, sub, start=0, end=None, side='left'):
703
709
-------
704
710
found : array of integer values
705
711
'''
706
- sub = str (sub )
712
+ sub = self ._obj .dtype .type (sub )
713
+
707
714
if side == 'left' :
708
715
method = 'find'
709
716
elif side == 'right' :
@@ -761,7 +768,7 @@ def index(self, sub, start=0, end=None, side='left'):
761
768
-------
762
769
found : array of integer values
763
770
'''
764
- sub = str (sub )
771
+ sub = self . _obj . dtype . type (sub )
765
772
766
773
if side == 'left' :
767
774
method = 'index'
@@ -837,6 +844,12 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=True):
837
844
if not (_is_str_like (repl ) or callable (repl )):
838
845
raise TypeError ("repl must be a string or callable" )
839
846
847
+ if _is_str_like (pat ):
848
+ pat = self ._obj .dtype .type (pat )
849
+
850
+ if _is_str_like (repl ):
851
+ repl = self ._obj .dtype .type (repl )
852
+
840
853
is_compiled_re = isinstance (pat , type (re .compile ('' )))
841
854
if regex :
842
855
if is_compiled_re :
@@ -906,4 +919,4 @@ def encode(self, encoding, errors='strict'):
906
919
else :
907
920
encoder = codecs .getencoder (encoding )
908
921
f = lambda x : encoder (x , errors )[0 ]
909
- return self ._apply (f , dtype = np .string_ )
922
+ return self ._apply (f , dtype = np .bytes_ )
0 commit comments