Skip to content

Commit

Permalink
TST: added unit tests from PR #1179 and copied docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Jul 15, 2012
1 parent 835476f commit d2f745a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 9 deletions.
53 changes: 44 additions & 9 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,25 @@ def str_count(arr, pat):
return _na_map(f, arr)


def str_contains(arr, pat):
def str_contains(arr, pat, case=True):
"""
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
Returns
-------
"""
regex = re.compile(pat)
if not case:
regex = re.compile(pat, re.IGNORECASE)
else:
regex = re.compile(pat)
f = lambda x: bool(regex.search(x))
return _na_map(f, arr)

Expand Down Expand Up @@ -308,7 +313,7 @@ def str_upper(arr):
return _na_map(lambda x: x.upper(), arr)


def str_replace(arr, pat, repl, n=0):
def str_replace(arr, pat, repl, n=0, case=True):
"""
Replace
Expand All @@ -320,12 +325,17 @@ def str_replace(arr, pat, repl, n=0):
Replacement sequence
n : int, default 0 (all)
Number of replacements to make from start
case : boolean, default True
If True, case sensitive
Returns
-------
replaced : array
"""
regex = re.compile(pat)
if not case:
regex = re.compile(pat, re.IGNORECASE)
else:
regex = re.compile(pat)
def f(x):
return regex.sub(repl, x, count=n)

Expand Down Expand Up @@ -599,7 +609,8 @@ def wrapper(self):
return self._wrap_result(result)

wrapper.__name__ = f.__name__
wrapper.__doc__ = f.__doc__
if f.__doc__:
wrapper.__doc__ = f.__doc__

return wrapper

Expand All @@ -610,10 +621,20 @@ def wrapper(self, pat):
return self._wrap_result(result)

wrapper.__name__ = f.__name__
wrapper.__doc__ = f.__doc__
if f.__doc__:
wrapper.__doc__ = f.__doc__

return wrapper

def copy(source):
"Copy a docstring from another source function (if present)"
def do_copy(target):
if source.__doc__:
target.__doc__ = source.__doc__
return target
return do_copy


class StringMethods(object):
"""
Vectorized string functions for Series. NAs stay NA unless handled
Expand All @@ -632,47 +653,61 @@ def _wrap_result(self, result):
return Series(result, index=self.series.index,
name=self.series.name)

@copy(str_cat)
def cat(self, others=None, sep=None, na_rep=None):
result = str_cat(self.series, others=others, sep=sep, na_rep=na_rep)
return self._wrap_result(result)

@copy(str_split)
def split(self, pat, n=0):
result = str_split(self.series, pat, n=n)
return self._wrap_result(result)

@copy(str_get)
def get(self, i):
result = str_get(self.series, i)
return self._wrap_result(result)

@copy(str_join)
def join(self, sep):
result = str_join(self.series, sep)
return self._wrap_result(result)

def replace(self, pat, repl, n=0):
result = str_replace(self.series, pat, repl, n=n)
@copy(str_contains)
def contains(self, pat, case=True):
result = str_contains(self.series, pat, case=case)
return self._wrap_result(result)

@copy(str_replace)
def replace(self, pat, repl, n=0, case=True):
result = str_replace(self.series, pat, repl, n=n, case=case)
return self._wrap_result(result)

@copy(str_repeat)
def repeat(self, repeats):
result = str_repeat(self.series, repeats)
return self._wrap_result(result)

@copy(str_pad)
def pad(self, width, side='left'):
result = str_pad(self.series, width, side=side)
return self._wrap_result(result)

@copy(str_center)
def center(self, width):
result = str_center(self.series, width)
return self._wrap_result(result)

@copy(str_slice)
def slice(self, start=None, stop=None):
result = str_slice(self.series, start, stop)
return self._wrap_result(result)

@copy(str_slice)
def slice_replace(self, i=None, j=None):
raise NotImplementedError

count = _pat_wrapper(str_count)
contains = _pat_wrapper(str_contains)
startswith = _pat_wrapper(str_startswith)
endswith = _pat_wrapper(str_endswith)
findall = _pat_wrapper(str_findall)
Expand Down
53 changes: 53 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,59 @@ def test_get(self):
expected = Series([u'b', u'd', np.nan, u'g'])
tm.assert_series_equal(result, expected)

def test_more_contains(self):
# PR #1179
import re

s = Series(['A', 'B', 'C', 'Aaba', 'Baca', '', NA,
'CABA', 'dog', 'cat'])

result = s.str.contains('a')
expected = Series([False, False, False, True, True, False, np.nan,
False, False, True])
assert_series_equal(result, expected)

result = s.str.contains('a', case=False)
expected = Series([True, False, False, True, True, False, np.nan,
True, False, True])
assert_series_equal(result, expected)

result = s.str.contains('Aa')
expected = Series([False, False, False, True, False, False, np.nan,
False, False, False])
assert_series_equal(result, expected)

result = s.str.contains('ba')
expected = Series([False, False, False, True, False, False, np.nan,
False, False, False])
assert_series_equal(result, expected)

result = s.str.contains('ba', case=False)
expected = Series([False, False, False, True, True, False, np.nan,
True, False, False])
assert_series_equal(result, expected)

def test_more_replace(self):
# PR #1179
import re
s = Series(['A', 'B', 'C', 'Aaba', 'Baca',
'', NA, 'CABA', 'dog', 'cat'])

result = s.str.replace('A', 'YYY')
expected = Series(['YYY', 'B', 'C', 'YYYaba', 'Baca', '', NA,
'CYYYBYYY', 'dog', 'cat'])
assert_series_equal(result, expected)

result = s.str.replace('A', 'YYY', case=False)
expected = Series(['YYY', 'B', 'C', 'YYYYYYbYYY', 'BYYYcYYY', '', NA,
'CYYYBYYY', 'dog', 'cYYYt'])
assert_series_equal(result, expected)

result = s.str.replace('^.a|dog', 'XX-XX ', case=False)
expected = Series(['A', 'B', 'C', 'XX-XX ba', 'XX-XX ca', '', NA,
'XX-XX BA', 'XX-XX ', 'XX-XX t'])
assert_series_equal(result, expected)

if __name__ == '__main__':
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
exit=False)

0 comments on commit d2f745a

Please sign in to comment.