Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0L committed May 26, 2019
1 parent 2de80ae commit bdbf845
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 122 deletions.
39 changes: 26 additions & 13 deletions xarray/core/accessor_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,10 @@ def get(self, i, default=None):
items : array of objects
'''
if default is None:
default = ''
default = self._obj.dtype.type('')

def f(x):
n = len(x)
if n <= i or i < -n:
return default
return x[i]
return self._apply(f)
obj = slice(i, i + 1)
return self._apply(lambda x: x[obj])

def slice(self, start=None, stop=None, step=None):
'''
Expand Down Expand Up @@ -130,12 +126,14 @@ def slice_replace(self, start=None, stop=None, repl=''):
-------
replaced : same type as values
'''
repl = self._obj.dtype.type(repl)

def f(x):
if x[start:stop] == '':
if len(x[start:stop]) == 0:
local_stop = start
else:
local_stop = stop
y = ''
y = self._obj.dtype.type('')
if start is not None:
y += x[:start]
y += repl
Expand Down Expand Up @@ -314,6 +312,7 @@ def count(self, pat, flags=0):
-------
counts : array of int
'''
pat = self._obj.dtype.type(pat)
regex = re.compile(pat, flags=flags)
f = lambda x: len(regex.findall(x))
return self._apply(f, dtype=int)
Expand All @@ -333,6 +332,7 @@ def startswith(self, pat):
An array of booleans indicating whether the given pattern matches
the start of each string element.
'''
pat = self._obj.dtype.type(pat)
f = lambda x: x.startswith(pat)
return self._apply(f, dtype=bool)

Expand All @@ -351,6 +351,7 @@ def endswith(self, pat):
A Series of booleans indicating whether the given pattern matches
the end of each string element.
'''
pat = self._obj.dtype.type(pat)
f = lambda x: x.endswith(pat)
return self._apply(f, dtype=bool)

Expand All @@ -374,7 +375,7 @@ def pad(self, width, side='left', fillchar=' '):
Array with a minimum number of char in each element.
'''
width = int(width)
fillchar = str(fillchar)
fillchar = self._obj.dtype.type(fillchar)
if len(fillchar) != 1:
raise TypeError('fillchar must be a character, not str')

Expand Down Expand Up @@ -491,6 +492,7 @@ def contains(self, pat, case=True, flags=0, regex=True):
given pattern is contained within the string of each element
of the array.
'''
pat = self._obj.dtype.type(pat)
if regex:
if not case:
flags |= re.IGNORECASE
Expand Down Expand Up @@ -530,6 +532,7 @@ def match(self, pat, case=True, flags=0):
if not case:
flags |= re.IGNORECASE

pat = self._obj.dtype.type(pat)
regex = re.compile(pat, flags=flags)
f = lambda x: bool(regex.match(x))
return self._apply(f, dtype=bool)
Expand All @@ -554,6 +557,9 @@ def strip(self, to_strip=None, side='both'):
-------
stripped : same type as values
'''
if to_strip is not None:
to_strip = self._obj.dtype.type(to_strip)

if side == 'both':
f = lambda x: x.strip(to_strip)
elif side == 'left':
Expand Down Expand Up @@ -703,7 +709,8 @@ def find(self, sub, start=0, end=None, side='left'):
-------
found : array of integer values
'''
sub = str(sub)
sub = self._obj.dtype.type(sub)

if side == 'left':
method = 'find'
elif side == 'right':
Expand Down Expand Up @@ -761,7 +768,7 @@ def index(self, sub, start=0, end=None, side='left'):
-------
found : array of integer values
'''
sub = str(sub)
sub = self._obj.dtype.type(sub)

if side == 'left':
method = 'index'
Expand Down Expand Up @@ -837,6 +844,12 @@ def replace(self, pat, repl, n=-1, case=None, flags=0, regex=True):
if not (_is_str_like(repl) or callable(repl)):
raise TypeError("repl must be a string or callable")

if _is_str_like(pat):
pat = self._obj.dtype.type(pat)

if _is_str_like(repl):
repl = self._obj.dtype.type(repl)

is_compiled_re = isinstance(pat, type(re.compile('')))
if regex:
if is_compiled_re:
Expand Down Expand Up @@ -906,4 +919,4 @@ def encode(self, encoding, errors='strict'):
else:
encoder = codecs.getencoder(encoding)
f = lambda x: encoder(x, errors)[0]
return self._apply(f, dtype=np.string_)
return self._apply(f, dtype=np.bytes_)
Loading

0 comments on commit bdbf845

Please sign in to comment.